7
votes

Goal

Using IIS 7.5 and the URL Rewrite 2.0 module, I want to rewrite the relative path "/myapp" to "/myapp-public", without a browser redirection.

What I've tried

I placed the following web.config file in wwwroot:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
    </system.web>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Rewrite to public" stopProcessing="true">
                    <match url="^myapp" />
                    <action type="Rewrite" url="/myapp-public" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Problem

Instead of rewriting the URL on the server side, IIS responds with a "301 Moved Permanently" which redirects the user to "/myapp-public". It is behaving as if I had used an action of type "Redirect" instead of "Rewrite".

Does anyone know why a "Rewrite" action would perform a redirect instead? Any thoughts on how to debug this issue further? Thanks!

1

1 Answers

12
votes

You are running into the courtesy folder redirect that IIS does for you, see here: http://support.microsoft.com/kb/298408/EN-US. To get around this, add the trailing slash to your rewrite url, like so:

<action type="Rewrite" url="/myapp-public/" />

But you have another issue too if these are in fact your actual urls. Your match url will match on your rewrite url and will cause an infinite loop, since ^myapp matches myapp-public. You can add the end of string ($) to your match url to fix this, i.e. ^myapp$ to prevent myapp-public from matching on it.