0

I want to redirect all requests from the old domain to a new domain. For example:

From:    oldhost.com/app1/houses?city=springs
To:      newhost.com/app1/houses?city=springs

I've tried using both the basic "HTTP Redirect" as far as "URL Rewrite" module with the same results. Redirect only works to a limited extent, as long as there is nothing past the first path segment:

oldhost.com/app1 - works
oldhost.com/app1/houses?city=springs - does not work

Here is the attempted URL Rewrite rule:

   <rule name="rule1" enabled="true" stopProcessing="true">
          <match url=".*" />
          <action type="Redirect" url="https://newhost.com/{C:1}" logRewrittenUrl="true" />
          <conditions>
               <add input="{HTTP_HOST}" pattern="oldhost.com(.*)" />
          </conditions>
    </rule>
Ya.
  • 1,671
  • 4
  • 27
  • 53
  • 1
    Does this answer your question? [IIS URL Rewrite not working with query string](https://stackoverflow.com/questions/19165676/iis-url-rewrite-not-working-with-query-string) – Stephen Ostermiller May 13 '22 at 22:01
  • 1
    Your redirect rule hits the common mistake 1, https://halfblood.pro/the-very-common-mistakes-when-using-iis-url-rewrite-module-a2ab7e4fee59 – Lex Li May 14 '22 at 01:51
  • @StephenOstermiller I've updated the question with a new rule using condition (this is actually one of the rules I've tried before). Still not working. – Ya. May 14 '22 at 14:24
  • @LexLi - Thx, please see my comment above. – Ya. May 14 '22 at 14:25
  • 1) Learn to use a private tab of your browser, as it won't cache old redirection. 2) Learn to use FRT to troubleshoot, https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules – Lex Li May 14 '22 at 15:23

2 Answers2

1

Got it to work with the following rule:

   <rule name="my_redirect" enabled="true" stopProcessing="true">
                    <match url=".*" />
                    <action type="Redirect" url="https://newhost.com/{R:0}" logRewrittenUrl="true" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="oldhost.com" />
                    </conditions>
   </rule>
Ya.
  • 1,671
  • 4
  • 27
  • 53
0

You can try this rule:

<rule name="test">
  <match url="^(.*)$" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^oldhost.com$" />
    </conditions>
  <action type="Redirect" url="https://newhost.com/app1{R:1}" />
</rule>
samwu
  • 3,857
  • 3
  • 11
  • 25