Scott Forsyth's Blog

Postings on IIS, ASP.NET, SQL Server, Webfarms and general system admin.

Cloud Resources

IIS Resources

IIS URL Rewrite – Redirect multiple domain names to one

Consider this a 2nd part to IIS URL Rewrite – rewriting non-www to www.  Reader Rubens asked about redirecting multiple domain names in a single rule.  That’s a good question and worth writing a part II blog post about it.

Here’s his question:

If there any generic way to do that if we have multiple domains with
multiple extensions?example: to (already done) to to
and maybe also to

Regular expressions are great for handling .com / .net / .org in a single condition.  You can do this using a “Condition input” check for {HTTP_HOST} with a “Pattern” of:

Note that the ^ marks the beginning of the pattern and the $ marks the end when using regular expressions. 
Additionally, to handle the, be sure to set the “Logical Grouping” to Match Any.  This allows any of the rules to cause the rule to match the condition and redirect.  After setting to Match Any, you can add as many domains as you want with a single rule with multiple conditions.
Let’s look at and .net.  Here’s a Pattern that will catch all situations:

This catches www and non-www and .com and .net.
The following screenshot shows conditions that will catch the following:
  • (1st condition)
  • (1st condition)
  • (2nd condition)
  • (2nd condition)
  • (2nd condition)
  • (2nd condition)
  • (3rd condition)
Note that the Action will redirect all of them to the same place (, which was Rubens’ goal.  Just be sure that no conditions match or you’ll create a loop.
The config that results should look like this:
        <rule name="Redirects to" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^domain.*(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^(www.)?mydomain2.(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^$" />
            <action type="Redirect" url="{R:0}" />
You can do this using wildcards too.  It would take more conditions.  It’s also possible to do this with rewrite maps, another feature of URL Rewrite.
Posted: Nov 30 2009, 02:40 PM by OWScott | with 17 comment(s)
Filed under: , ,


rubens said:

Thanks for the reply. The problem that I find in this second way is if you access the site with a directory it don't works.

Example OK:  -> -> -->

Example KO:  -> -> ->

# December 1, 2009 5:57 PM

OWScott said:

Rubens, that rule looks like it should work. In fact, I tested by coping your rule directly and all of those combinations work for me.  Are you sure you don't have another rule fighting with this rule?

# December 1, 2009 9:58 PM

OWScott said:

Drinn, if I understand you correctly, what you can do is setup a vdir between the two sites.  From your description, it sounds like those are 'sites'.  So, from each site, create a vdir to the other site's root folder and mark it as an application.

# February 18, 2010 8:24 PM

Corey said:

Is it possible to create a rule that would affect a site that has multiple host headers without having to enter each one separately?

Ex: and are both pointing to the same IIS7 entry, but display different content based on domain. Is there a single rule that could redirect them to and


# February 24, 2011 10:17 AM

OWScott said:

Hi Corey,

You're talking about 1 rule to rule them all. It may be possible, depending on what logic you want in the rules.  

The action can use backreferences.  {R:#} is a backreference for the URL field, and {C:#} is a back reference for the last condition in the list.  

So, if the condition is {HTTP_HOST} = ^mini-site-.\.com$, you can have an action rule of "http://www.{C:0}/{R:0}".  

Or if you want it more generic, you could do something like:

condition: {HTTP_HOST} = ^mini.*

rule action: http://www.{C:0}/R:0}

Or, another option is to create 2 conditions.  One is *not* HTTP_HOST = "^www\." and the bottom one is HTTP_HOST = ".*"

rule action: http://www.{C:0}/R:0}

That will redirect all domains that don't start with a www.

# February 24, 2011 11:17 AM

Chuck said:

Is this logic possible through TMG instead of IIS?

# June 6, 2011 2:48 PM

OWScott said:

Hi Chuck.  I haven't used TMG so I can't say.  Since it's a client-side redirect, it's technically possible to cause the redirect before it even reaches the web server, but I can't say one way or the other if TMG does rewriting like that.

# June 6, 2011 4:06 PM

OWScott said:

Hi Jack,

The way to do this is to set the URL match (the top section) to something like "^sales(.*)" with a redirect value of "{R:1}".  That will maintain the rest of the path in the redirect if they visit with something like

If you want to see more indepth walkthroughs, check out weeks 8-11 here:

# August 20, 2011 3:06 PM

OWScott said:

Hi Mac,

Check out my video series on this.  Week 8, the intro week for URL Rewrite, covers your situation in a short video which I hope that you find helpful:

# October 24, 2011 11:35 AM

OWScott said:

Speed Entry, your site isn't doing that now so I assume that you found a solution. The issue occurs because ASP.NET isn't aware of the subfolder and redirects to the full path when you click on that page that requires login and thus a redirect.

The solution is either to be more explicit in your path to the member-login page or to use outgoing rewrite rules for updating the paths.  Part 2 explains the second solution. The link is at the very top of this page.

# October 8, 2012 9:05 AM

JamesD said:

I'm attempting to redirect the exact match of to *Only if contains a url part, I'd like it to remain www3. E.g. would remain intact.

Thoughts? Thx!

# October 16, 2012 1:36 PM

ChrisF said:

I am trying to redirect a different domain name to a virtual directory maintaining the different domain in the url.

For example redirects to with the url remaining

I have host name binded to the website.

Is this possible?

# August 2, 2013 3:42 PM

OWScott said:

Hi Chris,

Yes, that is possible. A 'rewrite' rule will leave the URL untouched. Here is a blog post that covers that exact situation, along with a part II that addresses rewriting the outbound paths if you need it.

# August 4, 2013 1:14 AM

Sampada said:


How can I rewrite the the different domain headers such as, and to single domain.

There is a good example for it is

This all URLS pointed  to the same application.

Could you please let me know how can I implement it?

# September 20, 2013 5:47 AM

OWScott said:

Hi Sampada,

Are you trying to redirect so that they all using the same domain name, or point them all to the same site and keep the original domain name?

# September 24, 2013 11:20 AM

Naveen said:

My Question here is, whether the same URL Rewrite can be used to define for DNN websites

# October 29, 2013 3:31 AM

OWScott said:

Hi Naveen,

Absolutely, it's a good fit for URL Rewriting for DNN. DNN has its own URL management too so you can consider each, depending on your situation.

# November 1, 2013 5:18 PM
Leave a Comment