IIS rewrite rule to redirect specific domain url to different url on same domain

45,631

Solution 1

I got it to work this way:

<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>

Solution 2

Using the answer on this page, I was able to tweak a rule for myself. I also added query parameters. Wanted to post it here, in case it 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 bits of explanation:

To clear up some confusion, this "url" is the part after the first slash after the domain, and not the whole url. I'm going to put in this so that it gets any URL.

<match url=".*" />

Now, we'll add a condition because I had more than one web site on this computer, so I want to make sure this rule is applied to only one of them. I also used the wildcard instead of "(www.)?" because the wildcard will catch any subdomain.

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

And the last note is for folks who want to put multiple query string params in. You'll need to escape the ampersand between them, because it won't work if you don't:

<action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&amp;mode=form"
  appendQueryString="false" redirectType="Permanent" />
Share:
45,631
James Barry
Author by

James Barry

Updated on February 11, 2020

Comments

  • James Barry
    James Barry about 4 years

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

    <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 listens to several domains, thus above becomes a global rule for all domains. How do I make this specific to domain.com? Have tried changing match url and adding conditions but cannot get it to work. Thanks.

  • Jacques
    Jacques almost 7 years
    why do you use ^url1$ in your match node? Or is that intended to be replaced by what the OP had stated as example.com/url1? Would that make sense - wouldn't it be better to match all URLs i.e. ".*"? PS. The rest of what you said worked for me.
  • James Barry
    James Barry almost 7 years
    I wanted domain.com/subdir1 to redirect to domain.com/subdir2 but the website listened to multiple domains. I did not want this rule redirecting the other domains. url1 would be replaced by the starting segment (sub dir) you wanted to redirect. The condition was for a specific domain. And the action had the same domain and new segment.