Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
940 views
in Technique[技术] by (71.8m points)

iis - 301 Redirect one domain to another using web.config

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...