Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite not working with query string

I thought this was pretty straightforward, but it refuses to work. The old URL is

http://www.site.com/?q=node/17

It needs to redirect to http://www.site.com. I don't need to worry about wildcards, this is the only query string parameter I need to worry about. The rule I wrote looks like

<rule name="Node17" patternSyntax="ExactMatch" stopProcessing="true">
    <match url="http://www.site.com/?q=node/17" />
    <action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>

I can test the pattern inside of IIS and it matches, but when I hit the URL in a browser it doesn't redirect. Any thoughts?

like image 357
MyBrokenGnome Avatar asked Oct 03 '13 17:10

MyBrokenGnome


People also ask

How do I know if URL Rewrite is working?

Checking if the URL Rewrite module is installed To see if the URL Rewrite module is installed, open IIS Manager and look in the IIS group - if the module is installed, an icon named URL Rewrite will be present. The screenshot below shows an example of a server when the module is installed.

Does URL Rewrite require reboot?

Once installed you may need to reboot.


2 Answers

As described in Microsoft's documentation:

It is important to understand how certain parts of the URL string can be accessed from a rewrite rule.

For an HTTP URL in this form: http(s)://{host}:{port}/{path}?{querystring}

The {path} is matched against the pattern of the rule. The {querystring} is available in the server variable called QUERY_STRING and can be accessed by using a condition within a rule.

Rule conditions allow defining additional logic for rule evaluation... Rule conditions are evaluated after the rule pattern match is successful.

In the URL you wanted to rewrite as a redirect, your {host} = "www.site.com", {path} = "" and {querystring} = "q=node/17". So the {path} part in the URL you wanted to redirect is actually empty, and the rule you used in your question was matched against it and did not match.

Your solution is indeed valid, so I'll quote it here:

<rule name="Node17" stopProcessing="true">
    <match url=".*" />
<conditions>
    <add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.example.com" appendQueryString="False" />
</rule>
like image 135
RonyK Avatar answered Sep 20 '22 07:09

RonyK


Of course I figured it out soon after I posted. This does it, not really sure why the exactmatch wasn't working though.

<rule name="Node17" stopProcessing="true">
    <match url=".*" />
<conditions>
    <add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>
like image 24
MyBrokenGnome Avatar answered Sep 22 '22 07:09

MyBrokenGnome