12
votes

I use IIS 7.5 and URL Rewrite.

I have a website with the following file hierarchy:

webroot
webroot/LegacySite

Both webroot/ and legacy/ are separate App-Folders in IIS.


I need to rewrite my URLs so:

Below my Web.Conf (in the webroot folder) does not work properly, could you point out what I'm missing?

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="MyRole" stopProcessing="true">
                        <match url=".*" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^mysite.com" />
                            <add input="{PATH_INFO}" pattern="^\LegacySite\" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="\LegacySite\{R:0}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
1

1 Answers

20
votes

The following should work:

<rule name="MyRole" stopProcessing="true">
    <match url="LegacySite/(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^mysite.com$" />
    </conditions>
    <action type="Rewrite" url="/{R:1}" appendQueryString="true" />
</rule>

You might want to drop the conditional for checking the host name. Is that really important? Do you have any other domain names bound to that website for which you don't want the redirect to happen? It seems unnecessary. You probably only need:

<rule name="MyRole" stopProcessing="true">
    <match url="LegacySite/(.*)" />
    <action type="Rewrite" url="/{R:1}" appendQueryString="true" />
</rule>

I've added appendQueryString="true" to pass any (optional) query string parameters to the rewritten URL.