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

时光怂恿深爱的人放手 提交于 2019-12-04 03:58:25
James Barry

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

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