Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS 7.5 URL Rewrite - Rewrite a Folder from an URL

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:

  • If a request is http://mysite.co/LegacySite/page.aspx the URL will be rewritten to http://mysite.co/page.aspx

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>
like image 910
GibboK Avatar asked May 16 '12 09:05

GibboK


1 Answers

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.

like image 98
Marco Miltenburg Avatar answered Oct 14 '22 22:10

Marco Miltenburg