How to HTTP Redirect while maintaining subdomain using IIS 7

domain-name-systemiis-7redirectrewritesubdomain

My question is similar to
Redirect subdomain to subdomain on new domain except I'm using IIS 7. The application is ASP.NET framework 2.0.

I have a url cust1.olddomain.com and I need to redirect to cust1.newdomain.com.

I need to maintain the subdomain but I need the domain to change so that the user never sees olddomain.com, and I have X number of cust subdomains.

I am able to route everything where it needs to go using DNS, but I can't figure out how to change the domain so that olddomain.com is never displayed. I was hoping to make this a 301 redirect, but from what I have read so far I don't think that's possible.

Thoughts?

Best Answer

Yea, I answered my own question!

For IIS 7 you can install the URL Rewrite Module from the MS Web Platform Installer or download it separately. Once installed, this essentially gives you the power of mod_rewrite in IIS.

To solve my redirect issue I created a new redirect entry as shown below. The URL Rewrite module provides a user interface for creating the redirect which then writes the entry to the site's web.config, like many other IIS modules.

<rewrite>           
        <rules>
            <rule name="RedirectToNewDomain" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern="*.olddomain.com*" />
                </conditions>
                <action type="Redirect" url="http://{C:1}.newdomain.com{C:2}{PATH_INFO}" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>

What I am doing with this redirect is allowing all urls into the rule with the wildcard match url="*". Then I am conditionally pattern matching on the old domain. The asterisks in the conditional pattern match allow me to have back-references to whatever matches the pattern, which I then plug as {C:1} & {C:2} in the redirect action. It's the {C:1} back-reference that allows me to maintain my subdomain during the redirect and is the answer to the problem.

Related Topic