IIS URL Rewrite rule to redirect multiple domain names to one

IIS URL Rewrite: redirect di più nomi di dominio su un singolo hostname

One of the most important things you can do to improve your website Search Engine Optimization (SEO) is to comply with the Unique page URLs rule, meaning that each page should answer to one – and only one – URL request. This rule covers even homepages, implying that you can’t have your website answer to multiple domain nams or alias (ryadel.com, www.ryadel.com, ryadel.net, www.ryadel.net, ryadel.org, www.ryadel.org): you have to pick and use your favorite one and properly configure a 301 redirect for all the others.

The redirect we need there is, as said, a standard HTTP 301 – permanent, which can be obtained in two ways: delegate the whole thing to your service provider, hoping he has an interface tool to allow you to configure it, or set their IP to your own server and handle the redirect using your web service (IIS, Apache et. al). If you don’t have direct access to your web server/web service interface manager you’re forced to take the first path, otherwise you can – and you should – choose the latter: you can do that in a matter of minutes as long as you’ll follow these rather simple instructions.

 

URL Rewrite Module

If you’re using IIS 7.0 or above you can take advantage of the URL Rewrite Module, a handy IIS component which is downloadable for free from this page or by using the Microsoft Web Platform. The most recent version is 2.0, which looks – and works – almost identical to the previous ones. Once installed, you can check its presence by opening the IIS Admin Interface and looking for a new icon called URL Rewrite.

iis-url-rewrite

Now you can choose between using it by learning the GUI commands or manually set its rules in your website’s web.config file. Personally I have some issues with the GUI, which tends to become messy after more than few rules and it’s not ideal if you want to get the whole picture: in this post we’ll go for the web.config XML syntax, which is robust, solid and also well documented by Microsoft here.

Create, if it doesn’t exist already, a     section and copy & paste the following code. Be sure to replace YOURSITE with the hostname of your website and OTHERSITE1, OTHERSITE2 with the other domain names you want to 301 redirect to it.

The above rule will 301 redirect to www.YOURSITE.com all the following: YOURSITE.com, YOURSITE.net/org/eu/it, www.YOURSITE.net/org/eu/it, OTHERSITE1.com/net/org/eu/it, www.OTHERSITE1.com/net/org/eu/it, OTHERSITE2.com/net/org/eu/it and www.OTHERSITE2.com/net/org/eu/it. Not bad, isn’t it?

Let’s see how it works under the hood:  we’ve created an ECMAScript-based rule (row 4), allowing us to use the Regular Expression syntax to build highly versatile input patterns  (rows 7, 8, 9 and 10) to be checked against the HTTP_HOST header field contained in each request received by our website. The check will be performed using a MatchAny condition (row 6), meaning that the redirect will occur if at least one of them will match with the aforementioned HTTP_HOST.

Now, to the input pattern: as we’re already pointed out, they are all set to check the HTTP_HOST header field, which contains the domain name of any HTTP request (es. www.ryadel.com). The checks are as follows:

  • The first (row 7) will trigger if the domain name is YOURSITE.com (without the www).
  • The second (row 8) will trigger if the domain name, with or without www, has an extension of type .net, .org, .eu or .it (you can add other extensions if you need to).
  • The third (row 9) will trigger if the domain name, with or without www, belongs to the OTHERSITE1 (notice that the .com extension is also checked here).
  • The fourth (row 10) works just like the third, it just checks a different domain name.

ISAPI Rewrite 3

If you’re using an older version of IIS (lower than 7), you won’t be able to use the URL Rewrite module: you can get an almost-identical result by installing the great trial-ware software called ISAPI Rewrite 3 by HeliconTech. Its full documentation can be found here. Once installed, just create an .htaccess file in the main folder of your website and copy & paste the following text into it:

Again, be sure to replace YOURSITE, OTHERSITE1 and OTHERSITE2 with your actual domain names.

MOD_Rewrite

If you’re using Apache you can install the mod_rewrite module and use the same text al syntax of ISAPI Rewrite 3: as the latter is mostly a Windows/IIS porting of the first one.

That’s it for now: happy redirect!

 

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

2 Comments on “IIS URL Rewrite rule to redirect multiple domain names to one”

  1. I was searching for this issue and even though stack overflow didn’t give me the answer but finally i got here how to redirect multiple domains, can we use this for multiple urls into single url for a particular domain

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.