Question

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?

Was it helpful?

Solution 2

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>

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top