301 Redirect one domain to another using web.config

22,962

This is not really that umbraco related but I think what you want to do is this:

<rewrite>
  <rules>
    <rule name="redirect" enabled="true">
      <match url="(.*)" />
        <conditions>
          <add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
        </conditions>
      <action type="Redirect" url="http://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

Match all urls unless the host name part is exactly www.example.com - and redirect those to www.example.com/whatever.

Share:
22,962
jezzipin
Author by

jezzipin

Software developer at Tempcover

Updated on July 09, 2022

Comments

  • jezzipin
    jezzipin almost 2 years

    I have multiple domains pointing to one hosting location. I wish to establish one of the domains as my main domain and therefore I wish to perform a 301 redirect to this main domain whenever a user accesses my site from a secondary domain.

    For example:

    www.example.com

    This is my main domain. I want all other domains associated with my site to redirect to here.

    If a user comes in on:

    www.test.com or www.test.com/anypage etc.

    Then I want the user to be redirected to the example version of that page.

    How do I do this using the web.Config file of my application? The reason I ask is that usually my web hosting provider has a tool in their back office that allows me to setup this redirect however, our client has opted for a different hosting provider that do not provide such a tool.

    I have attempted to do this redirect using the following code but it doesn't seem to work:

    <rule name="Canonical Host Name" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^test\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.example.com/{R:1}}" redirectType="Permanent" />
    </rule>
    

    My application is an Umbraco powered site and so has several system.webServer entries in the web.config file. It may just be the case that I have entered this code in the wrong place but any help here would be greatly appreciated as I am only used to doing 301 redirects in .htaccess files.