Reputation: 1074
Running the following IIS URL Rewrite setup locally and can't get it to work (from web.config):
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="^tvb/" value="/tv/" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="StaticRedirectsRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{StaticRedirects: {REQUEST_URI}}" matchType="Pattern" pattern="(.+)" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
Trying to open the url http://localhost/tvb/ or http://localhost/tvb/?a=b returns a 404 error instead of redirecting me to http://localhost/tv (with or without query string).
I've found similar posts here on stackoverflow and I've tried what they recommended: recycling the app pool, restarting the IIS service, replacing REQUEST_URI with URL and/or REQUEST_FILENAME but none of these changes made a difference.
Can anyone see where I went wrong?
Upvotes: 1
Views: 4469
Reputation: 78
I had the same problem, and replacing REQUEST_URI with PATH_INFO fixed the issues.
Here is the link https://forums.iis.net/post/1883056.aspx
Reasoning: 'This is because REQUEST_URI contains the entire requested URL that includes the query string. Since the keys in the rewrite map do not contain the query string the map lookup fails.'
Upvotes: 0
Reputation: 1074
Well, I managed to find a solution for this myself, so here's what I did:
First, it seems rewriteMap does not support regular expressions, so this doesn't work:
<add key="^tvb/" value="/tv/" />
instead, it has to be like this:
<add key="/tvb/" value="/tv/" />
Also, it seems the IIS7 REQUEST_URI variable has changed behavior since the Url Rewrite 2.0 module (and documentation) was released.
Previously, REQUEST_URI only included the path of the url, without domain and query string.
Now, apparently REQUEST_URI in IIS7 works like the one in Apache, i.e. it includes the query string as well, so no wonder this part doesn't work:
<add input="{StaticRedirects: {REQUEST_URI}}" ... />
instead I had to change it to this:
<add input="{StaticRedirects: {SCRIPT_NAME}}" ... />
And now the redirects work, both with and without query strings!
Hope this helps someone.
Just found this on serverfault: IIS Rewrite, rewrite maps and query strings.
Upvotes: 5