301 Redirect one domain to another using web.config

浪尽此生 提交于 2019-11-29 06:07:48

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32648449/301-redirect-one-domain-to-another-using-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!