Search code examples
regexhttp-redirectiis-7.5

IIS7 Redirect using Rewrite module Issue


I have changed domains and am trying to redirect all requests except for a few URLs. However I can't seem to get it to behave as expected and everything gets redirected - I'm sure it must be something small - any help would be appreciated!

I need for all URLs to be redirected, except anything with the path "auth" or "ipet", as well as file.aspx including its querystring. The rest of olddomain.com should all redirect to newdomain.com.

<rule name="Redirect" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions trackAllCaptures="false" logicalGrouping="MatchAll">
    <add input="{HTTP_HOST}" pattern="www\.olddomain\.com" />
    <add input="{URL}" pattern="^file.aspx$" negate="true" />
    <add input="{URL}" pattern="^auth/?" negate="true" />
    <add input="{URL}" pattern="^ipet/?" negate="true" />
</conditions>
    <action type="Redirect" url="http://newdomain.com" />
</rule>

Solution

  • logicalGrouping="MatchAll" indicates all conditions must be true to do the redirect. Change logicalGrouping="MatchAny" and change match url.

    <rule name="Redirect" enabled="true" stopProcessing="true">
    <match url="www\.olddomain\.com" />
    <conditions trackAllCaptures="false" logicalGrouping="MatchAny">
        <add input="{REQUEST_URI}" pattern=".*?file.aspx$" negate="true" />
        <add input="{REQUEST_URI}" pattern=".*?auth/?" negate="true" />
        <add input="{REQUEST_URI}" pattern=".*?ipet/?" negate="true" />
    </conditions>
        <action type="Redirect" url="http://newdomain.com" />
    </rule>