1

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.

James Lawruk
  • 30,112
  • 19
  • 130
  • 137
  • That's a little bit vague... first you say.. > So for an incoming Url like this: /about?mobile=true&xyz=1 I want to > remove the mobile querystring but keep the xyz querystring. and then you have: > I don't want the (mobile, nomobile, etc.) querystrings removed from > the incoming request. If the Url is a 200, I want the underlying > ASP.NET Web applications to see the mobile querystring. Both times it's "incoming request". Please, specify the difference once again. – Andrew Sep 05 '12 at 01:03
  • Thanks, I will reword. When a server responds with a redirect, it sends an HTTP header called Location which contains the destination Url. I want the querystring removed from the Location value of the response. The querystring should not be removed from the request Url. – James Lawruk Sep 05 '12 at 14:03
  • I'd use smth like RewriteHeader Location: /([^?]+)?mobile=[^&]+&(.*) /$1?$2 – Andrew Sep 05 '12 at 23:07
  • HeliconTech itdelf has some documentation on this - http://www.helicontech.com/isapi_rewrite/doc/RewriteHeader.htm and you may also use search through their forum – Andrew Sep 05 '12 at 23:08

1 Answers1

2

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>
James Lawruk
  • 30,112
  • 19
  • 130
  • 137