Peter
Peter

Reputation: 864

IIS Rewrite module - how to redirect maintaining path and querystring but adding an extra querystring parameter?

I've been banging my head against a brick wall attempting to get some IIS redirect rules to work. I've searched and read stuff here on Stack Overflow and on IIS.net but it just doesn't work at all.

I'm trying it on my local (real) IIS, I have the rewrite 2.0 module installed and have tried doing a repair install on it. I've done an iisreset at an admin cmd line more times than I care to mention.

In my hosts file I have set-up 127.0.0.1 for the URL my.test.com.

What I want to achieve is given a sub-domain URL redirect to the main domain URL with an extra querystring parameter whilst maintaining the existing path and querystring values if they exist.

I setup 3 rules as follows that are in the root folder of the website:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="PathAndQueryString" enabled="true" stopProcessing="true">
                <match url="(/\w*)(\?\w*=\w*)([&amp;\w*=\w*]*)" />
                <action type="Redirect" url="https://www.test.com{R:1}{R:2}{R:3}&amp;param=value" appendQueryString="false" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_HOST}" pattern="my.test.com" />
                </conditions>
            </rule>
            <rule name="Querstring" enabled="true" stopProcessing="true">
                <match url="(\?\w*=\w*)([&amp;\w*=\w*]*)" />
                <action type="Redirect" url="https://www.test.com{R:1}{R:2}&amp;param=value" appendQueryString="false" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="my.test.com" />
                </conditions>
            </rule>
            <rule name="DomainOnly" enabled="true" stopProcessing="true">
                <match url=".*" />
                <action type="Redirect" url="https://www.test.com?param=value" appendQueryString="false" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="my.test.com" />
                </conditions>
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Unfortunately despite testing the patterns and confirming the {R:x} captures are correct when I try to test this using Edge, Chrome, Firefox and IE for all of them IIS acts like the rules don't exist.

Tests:

None of the above tests work, they get served without a redirect.

I've also tried putting the condition pattern used as part of the Match URL pattern but it didn't work with that either; in fact I moved it to the condition after reading a Stack Overflow post which said when rules are in the root of the site it doesn't include the host, but still nothing works.

Any help would be much appreciated.

UPDATE 1: For the path and querystring rule tried removing the bolded part from that start of this pattern (/\w*). Didn't have any effect.

UPDATE 2: Tried enabling the extremely temperamental "Failed Request Tracing" functionality and when it is actually working it says that none of the Match URL patterns are matching, except for the 3rd rule which has now started kicking in and redirecting but is not maintaining the path and other querystring params for obvious reasons.

UPDATE 3: On Edge and IE none of the rules work at all. On Chrome and FF the domain only redirect appears to be working - however if I disable the domain only rule and restart IIS it acts as though the rule is still there - FFS give me strength this crap is really starting to boil my blood now - this should not be that damn difficult.

Upvotes: 1

Views: 4047

Answers (1)

Peter
Peter

Reputation: 864

Before I give the answer, a shout out to Yuk Ding at Microsoft who responded to a copy of this post on the iis.net forums and provided most of the answer.

So here we go, if you want rewrite rules that redirects from 1 URL to another maintaining the path and querystring if they exist whilst at the same time adding on a hardcoded URL parameter with the appropriate & or ? then here are the rules you will need.

In the rules you are redirecting from my.test.com to www.test.com, but obviously you would need to replace those 2 URLs in the rules below as well as changed the hardcoded parameter "ExtraParam=SomeValue" to what you need.

<system.webServer>
<rewrite>
    <rules>
        <rule name="DomainOnly" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions trackAllCaptures="true">
                <add input="{HTTP_HOST}" pattern="my.test.com" />
                <add input="{REQUEST_URI}" pattern="/.+" negate="true" />
            </conditions>
            <action type="Redirect" url="http://www.test.com?ExtraParam=SomeValue" redirectType="Temporary" />
        </rule>
        <rule name="PathOnly" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions trackAllCaptures="true">
                <add input="{REQUEST_URI}" pattern="/.+" />
                <add input="{REQUEST_URI}" matchType="Pattern" pattern="\?.+" ignoreCase="true" negate="true" />
                <add input="{HTTP_HOST}" pattern="my.test.com" />
            </conditions>
            <action type="Redirect" url="http://www.test.com{C:0}?ExtraParam=SomeValue" redirectType="Temporary" />
        </rule>
        <rule name="Querstring" stopProcessing="true">
            <match url="(.*)" />
            <conditions trackAllCaptures="true">
                <add input="{REQUEST_URI}" pattern="/.+" />
                <add input="{QUERY_STRING}" pattern=".*" />
                <add input="{HTTP_HOST}" pattern="my.test.com" />
            </conditions>
            <action type="Redirect" url="http://www.test.com{C:0}&amp;ExtraParam=SomeValue" appendQueryString="false" redirectType="Temporary" />
        </rule>
    </rules>
</rewrite>

In my eyes the URL Rewrite 2.0 module has a bug. The basic crux is that you cannot match anything in the main match pattern you have to do everything with conditions. Thus the fact that you can set a pattern for the match is completely pointless because if you try to match anything other than (.*), i.e. match anything like I originally did it won't do what you want and will effectively cause head-bashing-brick-wall-ness.

The even more annoying thing is that in IIS Manager the UI for this is very unhelpful. You can test your patterns - and I did - and they will work, but all it's really testing is the regEx pattern.

What we really need is a test at the rules level. In other words you enter a URL and it tells you whether any of the rules match and which conditions pass or fail. Yes you can get this information if you enabled the failing request tracing rules but it's all a long winded phaff to enable and works in a haphazard fashion (just try clearing out the old logs and watch as it then stops logged until you turn it off and on again) for something that should be trivial.

Upvotes: 4

Related Questions