This post addresses the situation in which you want to redirect all subdomains to your main domain and you are using host headers in IIS. The main problem is the host header binding in IIS does not allow wildcards. Since you can not use wildcard host headers any subdomain requests will never reach your web site’s URL Rewrite processing. The following is the technique I use to do subdomain redirects on a web server hosting multiple sites using host headers.
First you’ll need to configure your DNS host to map all subdomains to your web site IP address. For www.abc-test1.com this will be something like *.abc.com. How you accomplish the DNS configuration will depend on who handles your DNS. If your DNS provider does not provide wildcard DNS entries you’ll want to find one who does.
Create a web site without a host header defined. This web site will use URL Rewrite to redirect subdomain requests to your main domain. I chose to call this web site Redirector. It should never respond to web requests, so it is essentially an empty web site. The binding for the redirector site will look like the following:
Your host header bindings for www.abc-test1.com and xyz-test2.com:
Your rewrite rules in the web.config file on the redirector site will look something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="abc-test1 Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*abc-test1\.com" />
<add input="{HTTP_HOST}" negate="true" pattern="www\.abc-test1\.com" />
</conditions>
<action type="Redirect" url="http://www.abc-test1.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="xyz-test2 Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*xyz-test2\.com" />
<add input="{HTTP_HOST}" negate="true" pattern="^xyz-test2\.com$" />
</conditions>
<action type="Redirect" url="http://xyz-test2.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The above rewrite rules will look the following in the GUI configuration:
Given the above configuration if you request any subdomains they will be permanently redirected to the main domain. For example: test.abc-test1.com, www2.abc-test1.com, abc-test2.com, and any variation will be 301 redirected to www.abc-test1.com. Likewise test.xyz-test2.com, www2.xyz-test.com, www.xyz-test2.com, and any variation will be 301 redirected to xyz-test2.com.
There are likely other ways to accomplish subdomain redirects, but this is working out for me. Hopefully this helps someone else as well.
Next entry: SSL with .NET SmtpClient use port 587 for the Comcast SMTP server
Previous Entry: Bypass UAC on Vista to edit the hosts file
Latest entries:
Create absolute URLs using ASP.NET MVC
Comments
Whoops… sorry, in the comment above <b>I didn’t mean Microsoft WebMatrix</b>, I meant the <a href="http://www.microsoft.com/web/downloads/platform.aspx">Microsoft Web Platform Installer</a>. My bad - I was in a rush to do something else. Microsoft WebMatrix is only a web dev tool like Dreamweaver, it has nothing to do with URL Rewrite (although it does come packaged in the Web Platform Installer).
Thanks for your comments, I've been meaning to respond, but I've been so busy weeks pass by like days :-) You are correct about the default website, I usually delete it. Regarding the use of the {R:1} back reference in the redirect. I'm pretty sure you want to use the back reference, otherwise the redirect to any page not on the main domain name will redirect to the root of the site. For example: a request for yourdomain.com/somepage will redirect to yourdomain.com/ if the {R:1} back reference is left out. You really want any inbound traffic to a particular page to redirect to the same page on the main domain, even if the external hyperlink is not using the main domain name. This will keep the SEO juice flowing through as you want from external links to your pages. Thanks again for you comments, I appreciate it.
Is have the following domainone.com (in dns it is a wildcard *.domainone.com) domaintwo.com (in dns it is a wildcard *.domaintwo.com) domainthree.com (in dns it is a wildcard *.domainthree.com) all these websites have multiple subdomains for example location1.domainone.com location2.domainone.com location1.domaintwo.com location2.domaintwo.com location1.domainthree.com location2.domainthree.com The wwwroot directory is empty and is going to be the redirector site to these vhosts however, what rules would I need to add to the Url Rewrite in IIS to achieve this? Thanks
My Links
Tags
Follow me
About
Powered by FoxBlog
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2011, Nathan Fox
This is one tutorial that I found incredibly invaluable. Since I can be such a perfectionist, one of the things I am always wondering about are mistyped URLs. As well, I want to have only one bound Hostname; the traditional one that starts out with "www" unless it is a subdomain. Using this tutorial allowed me to set everything up to perfection. There are just a few caveats that I would like to point out, some of which may be errors on my part. YMMV. First, the default web site has itself got an empty hostname binding -- but DO NOT USE IT for this tutorial’s purpose. I was banging my head against a brick wall until I removed the empty hostname binding for the default site (and only for HTTP - I left the others) and created a new web site with/for the empty hostname binding. Then, and only then, did everything work. Secondly, most sites that you want to resolve *all* subdomains to a plain “www” url need to have the starting and ending regular expression indicators for the “does not match…” entry. That is, the “does not match…” entry should be like this: ^www\.yourdomain\.com$ Thirdly, if you have a primary www site and a subdomain under it, place any wildcard entries for your primary www site after (below) that of the subdomain, otherwise your subdomain will become unreachable. Kind of a “no, duh?” suggestion, but I can easily see a less experienced sysadmin miss this one. Fourthly, if you want a URL to automatically forward to another URL, this is one awesome way to easily do so, just leave out the “does not match…” entry and you’re done! Every request for that URL will be redirected as desired. And finally, there appears to be no real reason/benefit to putting {R:1} after the URL in the redirection field. Did this with and without, and no observable difference (statistical tracking by the log files, perhaps? Or maybe breadcrumbs for Google Analytics?) So Thanks, Nathan, for this absolute gem of an article! BTW, it works on IIS7.5 on W2K8R2. The URL Rewrite, however, needs to be installed separately using <a href="http://www.microsoft.com/web/default.aspx">Microsoft Webmatrix</a>. And it’s hidden, to boot (do a search for “e” and everything comes up, including stuff not appropriate for your platform).
René Kabis - January 29, 2011