Scott Forsyth's Blog

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

.

  • Scott Forsyth

Hosting Needs

Training and Dev Labs

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:
mydomain.com to www.mydomain.com (already done)
mydomain.net to www.mydomain.com
www.mydomain.net to www.mydomain.com
etccc...
and maybe also
www.mydomain2.net to www.mydomain.com
etccc...
Thanks.


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:
^domain.(com|net|org)$

Note that the ^ marks the beginning of the pattern and the $ marks the end when using regular expressions. 
 
Additionally, to handle the mydomain2.net, 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 mydomain2.com and .net.  Here’s a Pattern that will catch all situations:
^(www.)?mydomain2.(com|net)$

This catches www and non-www and .com and .net.
 
The following screenshot shows conditions that will catch the following:
  • domain.com (1st condition)
  • domain.net (1st condition)
  • www.mydomain2.com (2nd condition)
  • www.mydomain2.net (2nd condition)
  • mydomain2.com (2nd condition)
  • mydomain2.com (2nd condition)
  • www.domain.net (3rd condition)
image
 
Note that the Action will redirect all of them to the same place (www.domain.com), which was Rubens’ goal.  Just be sure that no conditions match www.domain.com or you’ll create a loop.
 
The config that results should look like this:
 
<rewrite>
    <globalRules>
        <rule name="Redirects to www.domain.com" 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="^www.domain.net$" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </globalRules>
</rewrite>
 
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 21 comment(s)
Filed under: , ,

Comments

Twitter Trackbacks for IIS URL Rewrite ??? Redirect multiple domain names to one - Scott Forsyth's Blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 IIS URL Rewrite ??? Redirect multiple domain names to one - Scott Forsyth's Blog         [asp.net]        on Topsy.com

# November 30, 2009 6:04 PM

IIS URL Rewrite ??? Redirect multiple domain names to one – Scott … | Arabic names said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one &#8211; Scott &#8230; | Arabic names

# November 30, 2009 8:58 PM

IIS URL Rewrite ??? Redirect multiple domain names to one – Scott … · Flash Media said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one &#8211; Scott &#8230; &middot;  Flash Media

# November 30, 2009 10:00 PM

rubens said:

Hello,

   I've been testing to make a rule that redirects anything to the correct domain; the idea is that when something different than the main domain is entered; then is redirected to the main domain; the problem is that it don't works because I suppose I'm doing something wrong.

               <rule name="all to www.domain.com" enabled="True" patternSyntax="Wildcard" stopProcessing="true">

                   <match url="www.domain.com" negate="true" ignoreCase="true" />

                   <conditions logicalGrouping="MatchAny">

                       <add input="{HTTP_HOST}" pattern="*" />

                   </conditions>

                   <action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" />

               </rule>

# December 1, 2009 10:02 AM

rubens said:

I think I found it from one example of ruslany blog:

<rule name="www.mydomain.com" enabled="true" stopProcessing="true">

<match url="(.*)" />

<conditions>

<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com$" negate="true" />

</conditions>

<action type="Redirect" url="http://www.mydomain.com/{R:1}" redirectType="Permanent" />

</rule>

# December 1, 2009 12:53 PM

OWScott said:

Rubens.  Yes, that 2nd example should work well.  It will catch everything except that specific URL.  

# December 1, 2009 1:10 PM

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:

mydomain.com  -> www.mydomain.com

www.mydomain.net -> www.mydomain.com

mydomain.net --> www.mydomain.com

Example KO:

mydomain.com/directory1  -> mydomain.com/directory1

mydomain.com/directory1/ -> mydomain.com/directory1/

mydomain.com/directory1/index.html -> mydomain.com/directory1/index.html

# 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

IIS URL Rewrite ??? Redirect multiple domain names to one - Scott … Domain on Me said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one - Scott &#8230; Domain on Me

# December 2, 2009 3:51 AM

IIS URL Rewrite ??? Redirect multiple domain names to one – Scott … « Blogging said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one &#8211; Scott &#8230; &laquo;  Blogging

# December 13, 2009 12:17 AM

IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth's Blog said:

Pingback from  IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth&#39;s Blog

# January 26, 2010 11:50 PM

drinn said:

How can I set up the rules for this instance, let say

i have two separate web application on my IIS server.

http://www.webapp1.com

http://www.webapp2.com

then, I want something like when the users want to access webapp2 the URL should be

http://www.webapp1.com/webapp2/default.aspx

thanks in advance!

# February 18, 2010 8:22 AM

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: mini-site-A.com and mini-site-B.com 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 www.mini-site-A.com and www.mini-site-B.com?

Thanks!

# 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

Jack Smith said:

I have simple redirect that i want to create but hasnt been working successful for me...i basically want to redirect a specific path to another path  e.g. http://example.com/sales/ redirect to http://example/inside/sales/  can you assist?

Thanks

# August 18, 2011 4:56 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 "example.com/.../sales{R:1}".  That will maintain the rest of the path in the redirect if they visit with something like example.com/.../home.aspx.

If you want to see more indepth walkthroughs, check out weeks 8-11 here: dotnetslackers.com/.../LearnIIS7.

# August 20, 2011 3:06 PM

Mac said:

hi i want to redirect my home page for canonical issue

like example.com

to

http://www.example.com/

what should i do

# October 23, 2011 6:48 AM

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: dotnetslackers.com/.../LearnIIS7

# October 24, 2011 11:35 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)