IIS 7.5 URL Rewrite - Rewrite a Folder from an URL

22,324

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.

Share:
22,324
GibboK
Author by

GibboK

A professional and enthusiastic Senior Front End Developer. Listed as top 2 users by reputation in Czech Republic on Stack Overflow. Latest open source projects Animatelo - Porting to JavaScript Web Animations API of Animate.css (430+ stars on GitHub) Industrial UI - Simple, modular UI Components for Shop Floor Applications Frontend Boilerplate - An opinionated boilerplate which helps you build fast, robust, and adaptable single-page application in React Keyframes Tool - Command line tool which convert CSS Animations to JavaScript objects gibbok.coding📧gmail.com

Updated on July 05, 2022

Comments

  • GibboK
    GibboK almost 2 years

    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>