Question

How can I modify just the value of a specific QueryString parameter if it is equal to a specified value, using a rewrite rule? I only want to change this value, and do not want to affect the rest of the url or any other query string parameters.

eg. If query string parameter ID = '123' then I want to rewrite the value as 'abc'

And this should work regardless of the form of the URL:

http://mysite/page.aspx?ID=**123** should resolve to  http://mysite/page.aspx?ID=**abc**

http://mysite/?ID=**123** should resolve to  http://mysite/?ID=**abc**

http://mysite/page.aspx?name=bob&ID=**123** should resolve to  http://mysite/page.aspx?name=bob&ID=**abc**

http://mysite/page.aspx?name=bob&ID=**123**&age=33 should resolve to  http://mysite/page.aspx?name=bob&ID=**abc**&age=33
Was it helpful?

Solution

This rule will work for you.

<rule name="replace query string" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
      <add input="{QUERY_STRING}" pattern="(.*)(id=123)(.*)" />
    </conditions>
    <action type="Redirect" url="{R:0}?{C:1}id=abc{C:3}" appendQueryString="false" logRewrittenUrl="true" />
</rule>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top