IIS rewrite rules to redirect a specific domain URL to another URL in the same domain

I just want the IIS 7.5 rewrite rule to redirect http://www.domain.com/url1 to http://www.domain.com/url2 (same domain). This can be achieved:

<rule name="Redirect url" enabled="true" stopProcessing="true"> <match url="^url1" /> <action type="Redirect" url="http://www.domain.com/url2" appendQueryString="false" redirectType="Permanent" /> </rule> 

However, this website is listening on multiple domains, so the above becomes a global rule for all domains. How to do this for .com domain? They tried to change the URL of the match and add conditions, but they could not make it work. Thanks.

+8
redirect url-rewrite-module
source share
2 answers

I got it like this:

 <rule name="Redirect url1" stopProcessing="true"> <match url="^url1$" /> <conditions> <add input="{HTTP_HOST}" pattern="^(www.)?domain.com$" /> </conditions> <action type="Redirect" url="http://www.domain.com/url2" appendQueryString="false" redirectType="Permanent" /> </rule> 
+10
source share

Using the answer on this page, I was able to configure the rule for myself. I also added query parameters. Want to post it here if this helps someone:

 <!-- probably requires custom rewrite module, available through web platform installer --> <rule name="Redirect oldsite.com" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" /> </conditions> <action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&amp;mode=form" appendQueryString="false" redirectType="Permanent" /> </rule> 

Some pieces of explanation:

To eliminate some confusion, this “url” is the part after the first slash after the domain, not the entire URL. I am going to include this so that it gets any URL.

 <match url=".*" /> 

Now we add the condition because there were several websites on this computer, so I want to make sure that this rule only applies to one of them. I also used a wildcard instead of "(www.)?" because a wildcard will catch any subdomain.

 <conditions> <add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" /> </conditions> 

And the last note for people who want to add some query string parameters. You will need to avoid the ampersand between them, because it will not work if you do not:

 <action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&amp;mode=form" appendQueryString="false" redirectType="Permanent" /> 
+2
source share

All Articles