Remove specific querystrings from the location Url of a redirect response using ISAPI and IIS

StackOverflow https://stackoverflow.com/questions/12267213

Question

I have a few querystring keys ("mobile", "nomobile", etc.) that I do not want to be sent in any 301 redirect responses.

For example, let's say I have the Url /about-us that redirects to /about.

RewriteRule ^about-us$ /about [NC,L,R=301]

Rewrite rules by default keep querystings in the redirect Url. So for an incoming Url like this:

/about?mobile=true&xyz=1

If a redirect rule is applied, I want the server to respond with a location Url that has the mobile querystring removed from the redirect Url, but still containing the xyz querystring. So I want this request to return with this destination Url:

/about?xyz=1

I don't want the (mobile, nomobile, etc.) querystrings removed from the incoming request. If the Url results in a 200, I want the underlying ASP.NET Web applications to see the mobile querystring. This querystring removal should happen on the Location header (i.e. destination Url) of the redirect response.

I have thousands of ISAPI RewriteRules, so I don't want to apply a RewriteCond to every rule.

Is there an ISAPI rule or a custom module I can put somewhere to apply this logic globally to ISAPI generated redirects or to any redirect responses coming out of IIS? Thanks for your help.

Était-ce utile?

La solution

Using Url Rewrite in IIS, you can create rules to modify outbound response headers. Below are the rules generated from the Url Rewrite tool:

<system.webServer>
 <rewrite>
  <outboundRules>
    <clear />
    <rule name="Remove nomobile from location">
      <match serverVariable="RESPONSE_Location" pattern="^(.*)\?nomobile(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
      <action type="Rewrite" value="{R:1}?{R:2}" />
    </rule>  
    <rule name="Remove mobile=true from location">
      <match serverVariable="RESPONSE_Location" pattern="^(.*)\?mobile(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
      <action type="Rewrite" value="{R:1}?{R:2}" />
    </rule>  
    <rule name="Replace &amp;">
      <match serverVariable="RESPONSE_Location" pattern="^(.*)(\?&amp;)(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
      <action type="Rewrite" value="{R:1}?{R:3}" />
    </rule>
    <rule name="Remove empty ?" enabled="true">
      <match serverVariable="RESPONSE_Location" pattern="(.*)\?$" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
      <action type="Rewrite" value="{R:1}" />
    </rule>
  </outboundRules>
 </rewrite>
</system.webServer>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top