Configuring IIS rewrite module to redirect any host name to a specific host name

8,428

Remove the whole conditions node from your rule.

You have the same pattern in the condition as in the rule which makes it redundant, except for the negate="true" attribute which is the reason why it didn't work.

The rule matches everything, but then the condition blocks everything, so nothing is redirected.

Only use conditions if you really need them to further limit requests matched by the rule.

Share:
8,428

Related videos on Youtube

ron
Author by

ron

Updated on September 18, 2022

Comments

  • ron
    ron over 1 year

    I have a website whose name I am changing from example.com to newexample.com. My thought process (please comment on this as well, as my approach may be incorrect) was to configure permanent a redirect from http:(s)://.example.com/ (I am not using '' in the literal regex sense here) to https://www.newexample.com/.

    In the rewrite rules section I took the following approach:

    <rewrite>
      <rules>
        <rule name="Redirect to newexample.com">
          <match url=".*" />
          <conditions logicalGrouping="MatchAny">
            <add input="{HTTP_HOST}" pattern=".*" negate="true" />
          </conditions>
          <action type="Redirect" url="https://www.newexample.com/{R:0}" redirectType="Permanent"/>
        </rule>
      </rules>
    </rewrite>
    

    The way I understand the above rules is that ANY host name will get redirected to "https://www.newexample.com/whatever", leaving all other aspects of the requested url as-is, but I am not getting redirected using any combination that I've tried so far.

    • Peter Hahndorf
      Peter Hahndorf over 8 years
      take out the condition
    • ron
      ron over 8 years
      Works! Sure is a more simple way to look at this... If you put your comment as an answer I'll mark it as accepted.