redirect subfolder to subdomain/subfolder using IIS URL Rewrite

11,921

This is what I ended up with.

First rule just redirects to a trailing slash if the URI is www.mydomain.com/application1.

Second rule redirects to the subdomain ignoring case, preserving querystring and treats it as a permanent redirect.

<rule name="Trailing Slash Redirect" stopProcessing="true">
    <match url="^(application1|application2)$" ignoreCase="true" />
    <action type="Redirect" url="{R:0}/" redirectType="Permanent" />
</rule>
<rule name="Http App Redirect" stopProcessing="true">
    <match url="^(application1/.*|application2/.*)$" ignoreCase="true" />
    <action type="Redirect" url="http://app.mydomain.com/{R:0}" redirectType="Permanent" />
</rule>

If I have the need for redirects to Https, I will create another rule.

Share:
11,921

Related videos on Youtube

Matt Demler
Author by

Matt Demler

Work as a software developer for a leading utility company in New Zealand. Background in .NET based technologies but am enjoying learning more about Javascript frameworks like Node.js, Angular, etc

Updated on September 16, 2022

Comments

  • Matt Demler
    Matt Demler over 1 year

    I am looking at using IIS Rewrite Module 2.0 to redirect some broken application links.

    Previously www.mydomain.com was our corporate site and www.mydomain.com/application1, www.mydomain.com/application2 were other applications our customers used. They were all on the same server.

    Now we are having our corporate site updated and moved to another server. So www.mydomain.com and mydomain.com resolve to a different server. As part of this work, a subdomain of app.mydomain.com was created to house the customer applications still residing on our server.

    What I want to be able to do is install the rewrite module on the new corporate site's server and redirect www.mydomain/application1 or mydomain.com/application1 to app.mydomain.com/application1.

    I have the list applications so maybe using a rewriteMap is the way to go? That would prevent any redirects like www.mydomain.com/newcorporateurl being wrongly redirected. I also need to preserve subfolders and querystrings (due to people having browser bookmarks) like www.mydomain.com/application1/index.aspx?id=1 needs to go to app.mydomain.com/application1/index.aspx?id=1

    Any help putting this together would be great! Even just getting the basic config sorted so I can have a play with it and understand it a bit more. I have been browsing other questions and resources but am still struggling how to put this together

    Update 1

    The below doesn't work but hopefully it demonstrates what I am trying to do. I have about 15 or applications I need to redirect in this way.

    <rewrite>
        <rules>
            <rule name="application1 rule" stopProcessing="true">
                <match url="(www\.)?mydomain\.com(:80)?/application1/?" />
                <action type="Redirect" url="app.mydomain.com/application1/" appendQueryString="false" />
            </rule>
            <rule name="application2 rule" stopProcessing="true">
                <match url="(www\.)?mydomain\.com(:80)?/application2/?" />
                <action type="Redirect" url="app.mydomain.com/application2/" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>
    

    Update 2

    I have this working to an extent

    <rewrite>
        <rules>
            <clear />
            <rule name="application1 rule" stopProcessing="true">
                <match url="application1(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="mydomain.com" />
                </conditions>
                <action type="Redirect" url="http://app.mydomain.com/application1{R:1}" />
            </rule>
        </rules>
    </rewrite>
    

    This will redirect

    www.mydomain.com/application1 -> app.mydomain.com/application1 www.mydomain.com/application1/index.aspx -> app.mydomain.com/application1/index.aspx

    The issue I want to resolve now is this.

    The new site at www.mydomain.com is a CMS. With the above rule, it means the CMS can't contain 'application1' at all in the URI.

    Real world example is 'commercial'

    Can someone please help refining my rule so the following is true

    www.mydomain.com/commercial -> app.mydomain.com/commercial www.mydomain.com/commercial/foo/index.aspx -> app.mydomain.com/foo/index.aspx

    www.mydomain.com/tv-commercials/list -> www.mydomain.com/tv-commercials/list www.mydomain.com/commercial-property/list -> www.mydomain.com/commercial-property/list