jallen
jallen

Reputation: 612

IIS Rewrite to remove certain querystring parameters and redirect to the original requested host

I need to write a redirect to strip off parameters when a certain one exists, but I would like the redirect to be the same as the incoming url, just without the parameters. This is what I had and it doesn't work. I thought if I put the request URI in there without appending the querystring, then it would work but it results in a loop. Has anyone done this before?

<rule name="Remove parameters" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions trackAllCaptures="true">
  <add input="{QUERY_STRING}" pattern="page=([0-9]+)" />
</conditions>
<action type="Redirect" url="{REQUEST_URI}" appendQueryString="false" />

Upvotes: 3

Views: 9392

Answers (2)

JDandChips
JDandChips

Reputation: 10110

The problem with jallen's answer is that the whole query string will be removed, which may not be ideal if you want to maintain certain parts of it. An alternative would be as follows:

<rule name="Remove paging parameters" stopProcessing="true">
    <match url="(.*)?$" />
    <conditions trackAllCaptures="true">
        <add input="{QUERY_STRING}" pattern="(.*)(page=.+)(.*)" />
    </conditions>
    <action type="Redirect" url="{C:1}{C:3}" appendQueryString="false" />
</rule>
  • C:1 = Everything before the matching page parameter
  • C:2 = Is the match itself, and what we want to exclude
  • C:3 = Everything after the matching page parameter

Hence, we use the redirect {C:1}{C:3} to exclude only the page query string.

Upvotes: 9

jallen
jallen

Reputation: 612

Got it with the following:

<rule name="Remove paging parameters" stopProcessing="true">
<match url="(.*)?$" />
<conditions trackAllCaptures="true">
  <add input="{QUERY_STRING}" pattern="page=([0-9]+)" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" />

Upvotes: 4

Related Questions