Search code examples
http-redirectiisurl-rewritingiis-7.5iis-10

IIS URL Rewrite + Redirect + does not match the querystring


I need to redirect to another website when the url contains www. and the query-string does not match the specfic value. I am always getting redirected irrespective of the condition

 <rewrite>
                <rules>
                    <rule name="Redirect to Landing Page" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{HTTP_Host}" pattern="www.dev.MyWebpage.com/MCPrivacyNotice" negate="true" />
                        </conditions>
                        <action type="Redirect" url="https://www.myWebPage.com" />
                    </rule>
                </rules>
            </rewrite>

Solution

  • As you described, so you need to redirect when

    • Domain is www.dev.MyWebpage.com
    • Request uri is /MCPrivacyNotice

    So I think this may be your answer

        <rule name="Redirect to Landing Page" stopProcessing="true"> 
            <match url="(.*)" /> 
                <conditions logicalGrouping="MatchAll"> 
                    <add input="{HTTP_HOST}" pattern="www.dev.MyWebpage.com" /> 
                    <add input="{REQUEST_URI}" pattern="/MCPrivacyNotice" negate="true" /> 
                </conditions> 
            <action type="Redirect" url="myWebPage.com" /> 
        </rule>
    

    Note that logicalGrouping="MatchAll" to match all condition. In your question and your update you used logicalGrouping="MatchAny" that means every request from domain www.dev.MyWebpage.com will be redirected

    One more thing, /MCPrivacyNotice is {REQUEST_URI} or PATH_INFO not QUERY_STRING you should choose the right module. Check this for detail https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

    Hope this helps