0

i'm trying to force a url redirect (with the url rewrite module but for some reason it doesn't seem to be willing to match..)

my use case is as follows: url: http://somesite.domain.com/?test=4

Now I'm trying to redirect this when it matches ?test=4 to http://somesite.domain.com/subsite/?test=5 I've tried this in multiple ways aleady by having a matching pattern of ^\?test=4 (on both the match url or the condition)

For some reason tho it isn't willing to trigger.

Is there something obviously wrong with a matching regex pattern of \?test=4 or should this work?

Full web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="^\?test=4" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="\?test=4" />
                    <action type="Redirect" url="/subsite/?test=5" appendQueryString="true" />
                    <conditions>
                    </conditions>
                </rule>
                <rule name="2" stopProcessing="true">
                    <match url="^$" />
                    <conditions logicalGrouping="MatchAny">
                    </conditions>
                    <action type="Redirect" url="/subsite/?test=5" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration> 

I'm fairly now to the url rewrite module so I'm going a bit by trial and error.. Any tips would be nice!

Dirk R.
  • 171
  • 1
  • 13
  • `match` tag only analyzes incoming requests' URL portion. If you want to analyze query strings (things behind ?) you have to use valid conditions. – Lex Li Dec 10 '19 at 14:18
  • 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) – Lex Li Dec 10 '19 at 14:18

1 Answers1

0

As far as I know, the url match will not match the query string. So your url rewrite rule will not work.

If you want to check the querystring, I suggest you could use IIS url rewrite querystring condition.

More details, you could refer to below url rewrite rule:

            <rule name="MatchQueryString" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{QUERY_STRING}" pattern="test=4" />
                </conditions>
                <action type="Redirect" url="/subsite/?test=5" appendQueryString="false" />
            </rule>

Result:

enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • That seems to do 95% of what I'm trying to do :) I'm just wondering whether i can do it similar to lets say somesite.domain.com/?test=4&other=12 to redirect to somesite.domain.com/subsite/?test=5&other=12. I tried this with the redirect and append querystring but then you ofcourse get somesite.domain.com/subsite/?test=5?test=4 i'm thinking rewrite instead of redirect here as I'm unsure where each parameter will be.. could be that the test is the first but also the other – Dirk R. Dec 17 '19 at 13:37
  • Would that work when I match the condition of the query_string on test=4&other=(.*) and then perform a redirect to /subsite/?test=5&other={C:1} ? This way the other variable can change but I would still be stuck on the order of the variables.. I would basicly like to find a way to pull specific patterns from the original url and then reconstruct it myself. – Dirk R. Dec 17 '19 at 13:44