Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS ReWrite Rule to remove query string when contains specific specific query string

Looking to remove a particular query string parameter. Folder name can be different and length of the specs param can vary with any combination of numbers. Whenever there is a specs parameter, regardless of the value, strip that parameter and redirect to http://example.com/folder

Example Inputs:

  • http://example.com/folder1?specs=10,13,14,18,20,29
  • http://example.com/folder2?specs=14,18,20

Would redirect to (respectively):

  • http://example.com/folder1
  • http://example.com/folder2

Do not strip any other query string params. i.e. http://example.com/folder1?page=1 would not get redirected.

Rule Tried, not working, despite seeming like it would when using the IIS rewrite rules test tools:

<rule name="SpecsSpiderCrawl" stopProcessing="true">
  <match url="(\/\/.*\/)(.*)\?" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="specs=.*" />
  </conditions>
  <action type="Redirect" url="http://{HTTP_HOST}/{R:2}" appendQueryString="false" redirectType="Permanent" />
</rule>
like image 886
crichavin Avatar asked Dec 16 '16 23:12

crichavin


1 Answers

I was making it too hard. This worked:

<rule name="SpecsSpiderCrawl" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="specs=.*" />
  </conditions>
  <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" appendQueryString="false" redirectType="Permanent" />
</rule>
  • {HTTP_HOST} is just the example.com portion of the uri
  • {R:0} will get the folder name
  • appendQueryString="false" will remove the entire query string (which is fine for my use case.
like image 92
crichavin Avatar answered Nov 15 '22 08:11

crichavin