2

On my website, I have a situation where my URL sometimes get a querystring parameter appended by a system outsite ours.

So instead of looking like this: http://www.domain.com?myquery=blah or http://www.domain.com, it has the url http: http://www.domain.com?myquery=blah&theirpara=blah or http://www.domain.com?theirpara=blah .

When a user visits with the "theirpara" parameter, I would like to make a 301 redirect to the URL without it.

I've tried using the URL rewrite module, but not really getting anywhere. It would be nice to do it at IIS/web.config level instead of Response.RedirectPermanent if possible.

I thought it would be good to setup up using a rule (URL write module), but to be honest, I've no idea how to fix this issue. I am using the following rule to remove the trailing slash, but not sure how to modify it to this need.

<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" stopProcessing="true">
  <match url="(.*)/$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

Any ideas on how to set it up?

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182
  • What's wrong with their parameter, that you want to remove it? – Uriil Nov 10 '12 at 13:19
  • Uriil: Search engine optimization purposes :) It gets indexed in Google, giving me duplicate content. It could be fixed with canonical url's, but I would like both solutions – Lars Holdgaard Nov 10 '12 at 13:38

2 Answers2

5

If you are looking for a pretty generic rule that removes theirpara but keeps any other query string parameters then you will have to handle cases where

  1. It is alone: ?theirpara=123 -> /
  2. It is first: ?theirpara=123&ourparams=456 -> ?ourparams=456
  3. It is in the middle: ?our1=123&theirpara=456&our2=789 -> ?our1=123&our2=789
  4. It is last: ?ourparams=123&theirpara=456 -> ?ourparams=123

I wrote a rule that handles these cases but in case 4 a trailing ampersand is left, I hope it is enough to get you started though.

<rule name="Redirect on theirpara" stopProcessing="true">
    <match url="^(.*)$" />
        <conditions>
            <add input="{QUERY_STRING}" pattern="^(.*)theirpara=([^=&amp;]+)&amp;?(.*)$" />
        </conditions>
    <action type="Redirect" url="/{R:0}?{C:1}{C:3}" appendQueryString="false" />
</rule>
strmstn
  • 852
  • 6
  • 10
  • I have try your rules but it's not working for me. When I try to test pattern it's show an error. http://prntscr.com/nf4wil – Kalpesh Boghara Apr 22 '19 at 06:54
  • @KalpeshBoghara. I think you shouldn't use & in that dialog. Just using & should be enough. – strmstn Apr 22 '19 at 17:08
  • My pattern us **^(.*)theirpara=([^=&]+)&?(.*)$** but website contain ? and & at last. ex: http://example.com? http://example.com?otherparam=valueofother& – Kalpesh Boghara Apr 24 '19 at 09:00
  • What happen if **theirpara** is not into URL. In my case throw an error, can't able to access website url without **theirpara** – Kalpesh Boghara Apr 26 '19 at 12:21
1

I don't have an IIS to hand but the following should be working:

Change match to

<match url="(.*)&theirpara=blah(.*)" />

and redirect to

<action type="Redirect" redirectType="Permanent" url="{R:1}{R:2}" />

If you want it to work not only on the plain domain you should remove the conditions accordingly.

Daniel Calliess
  • 861
  • 6
  • 12