Scott Forsyth's Blog

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

IIS URL Rewrite – rewriting non-www to www

If you’re using IIS 7.0 (or 7.5), URL Rewrite is a valuable tool, well worth installing and using.

One common use of URL Rewrite is redirecting http://domain.com to http://www.domain.com.  Many people are doing this for search engine optimization (SEO) so that search engines only see the one site, rather than two sites.  The goal is to set a permanent 301 redirect.

You can download URL Rewrite from http://www.iis.net/expand/URLRewrite.  For this walkthrough and screenshots I’ll use URL Rewrite 2.0 RC1, but everything that I’ll cover also works for version 1.0 and 1.1.

URL Rewrite works at the global level, or site level (or application level for that matter).  Where you apply it is really up to how you manage your server.  Either will work for a domain name redirect like this.

You can choose to create the rules using IIS Manager, or using a text editor and updating web.config directly.  I’ll show both, starting with IIS Manager.

Let’s get started.  First, open IIS Manager and double-click on the “URL Rewrite” icon.

image

Next, click on “Add Rules…” from the Actions pane.

Here you’ll have a choice from a few wizard options, and with URL Rewrite 2.0 you can also create outbound rules.  Create a Blank rule (inbound rules).

image

Give your rule a good friendly “Name”.  I’ll call mine “Redirect domain.com to www”.

In the “Using” dropdown box you can choose between Regular Expressions and Wildcards.  Use wildcards if you aren’t familiar with regular expressions since they are much more intuitive.  However, if you later need to create more complex rules, regex may be necessary.

For this demo select WildcardsHowever, I’ll include instructions for those wanting to use regular expressions.

Enter * for the “Pattern”.  That means anything qualifies.  We’ll use a condition later instead of matching to the URL.  (for Regular Expressions, use .*).

Now expand the “Conditions” section and click “Add”.  In the “Add Condition” dialogue enter the following:

Condition input: {HTTP_HOST}
Check if input string: Matches the Pattern
Pattern: domain.com
(for regex, enter ^domain.com$)
Ignore case: checked

image

Click OK.

Finally, it’s time to set the Action.

In the Action section make sure that the “Action Type” is set to Redirect

For the “Action Properties”, enter http://www.domain.com/{R:0}.  The {R:0} retains the existing URL so if someone typed something like http://domain.com/aboutus it would retain the aboutus as it adds the www.

Be sure that the “Append query string” remains checked so that the querystring part is also retained.

Also, be sure that the “Redirect Type” is set to Permanent (301), which is what the search engines like.  This tells the search engines to do a permanent redirect, use the new location and ignore the previous location.

image

Finally, Apply the rule and test!

Using a Text Editor

You can also create this rule manually by adding the following to your site’s web.config (or applicationHost.config if you set this at the server level).

In the <system.webServer> section of your web.config, add the following:

Wildcards

<rewrite>
    <rules>
        <rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="domain.com" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

 

Save and you should be set.

Or, if you prefer Regular Expressions, use this instead:

Regular Expressions

<rewrite>
    <rules>
        <rule name="Redirect domain.com to www" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^domain.com$" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

This is just the start to great SEO, but it’s a common step and one that I hope you find helpful.

See Part II on how to redirect multiple domain names to a single domain name: http://weblogs.asp.net/owscott/archive/2009/11/30/iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx

Posted: Nov 27 2009, 05:55 PM by OWScott | with 17 comment(s)
Filed under: , ,

Comments

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

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

# November 28, 2009 5:09 AM

Jason Haley said:

Interesting Finds: November 28, 2009

# November 28, 2009 8:27 AM

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

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

# November 28, 2009 10:35 AM

Twitter Trackbacks for IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth's Blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 IIS URL Rewrite ??? rewriting non-www to www - Scott Forsyth's Blog         [asp.net]        on Topsy.com

# November 28, 2009 12:36 PM

r4 dsi said:

Wow it good to know that using IIS Manager there are so many new options available. I normally like my net history categorized. Yes its just a step towards great SEO.

# November 30, 2009 5:54 AM

rubens said:

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.

# November 30, 2009 3:19 PM

OWScott said:

That's where regular expressions and multiple conditions come in.

To get you started, if you switch to regular expressions and use the example in my blog, then for the Condition Pattern, rather than ^domain.com$, switch it to ^domain.(com|net)$.  That will catch both .com and .net.

If you are redirecting them all to the same domain it's easy.  You don't need to update the Redirect URL.

If you do want to retain the .com or .net you can use {C:1} instead of com the redirect URL, but that's likely not what you're worried about.

To get mydomain2.net, just switch the Logical grouping dropdown in the conditions to "Match any" and add as many conditions as you want.  As long as any of them catch, it will redirect.

Obviously make sure that you don't put a condition that is the same as the redirect URL, otherwise you'll get a loop.

Note, you can do this with wildcards instead of regular expressions.  Just create a new condition for each of .com and .net and mydomain2.net.

# November 30, 2009 3:48 PM

rubens said:

Thanks a lot for the reply. I'm testing things right now and succeeded making it work with 8 rules.

I use only the web.config code since it's quicker.

If I want to make a rule that redirects any domain (www or non www; .net.org) to only one how would you do it?

Thanks,

# November 30, 2009 4:50 PM

OWScott said:

Hi Rubens.  I figured it would be worth answering that with a blog post where I can include screenshots: weblogs.asp.net/.../iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx.  Hopefully that helps.

# November 30, 2009 5:43 PM

IIS URL Rewrite ??? Redirect multiple domain names to one - Scott Forsyth's Blog said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one - Scott Forsyth&#39;s Blog

# November 30, 2009 5:47 PM

rubens said:

I appreciate that very much !!!!

Thanks.

# November 30, 2009 6:12 PM

Scott Forsyth's Blog said:

Consider this a 2nd part to IIS URL Rewrite – rewriting non-www to www .&#160; Reader Rubens asked about

# November 30, 2009 6:27 PM

IIS URL Rewrite ??? Redirect multiple domain names to one | I love .NET! said:

Pingback from  IIS URL Rewrite ??? Redirect multiple domain names to one | I love .NET!

# November 30, 2009 7:44 PM

nidhi tiwari said:

if we create web.config file on client's site becoz it was not there thn how can we use it for url redirection from www url to non www url and vise versa

# December 10, 2009 7:32 AM

Scott Forsyth said:

As long as your server (or host) supports URL Rewrite, then you can add the config directly to web.config in the <system.webServer> section and it should work for you.  It needs to be installed on the server for it to take though.

# December 10, 2009 9:21 AM

David Moore said:

I am a web host and have multiple sites on the server. I am using host headers to manage most of the websites. I have been successful in setting up this rule for the default website where upstateweb.com will redirect to www.upstateweb.com. The problem comes when I set up the same rule for any of the other domains. The domain name does not redirect and pulls up upstateweb.com. In otherwords, hollandflora.com does not redirect to www.hollandflora.com, but instead just stays hollandflora.com and shows the content on hollandflora.com. (And it is not a DNS issue. Both A records point correctly.)

Any suggestions?

# January 31, 2010 10:52 PM

OWScott said:

Hi David.  My best guess is that another rule catches first and is set to stop processing.  That will prevent rules further down from working.

A good test to see if the conditions work is to set the action to a custom response code of 500 0 and Test / Test for the text.  That's a quick and easy way to confirm that it's working.  Try moving the rule to the top of the list and see if it catches then.  If it still doesn't, try a quick 'break test' by stopping the website to confirm that it's bound to that site (it sounds like it is).  Also confirm that the rule type it wildcards or regular expressions... whatever you're expecting.  Being set to the opposite as you expect could cause rules that don't work.

# February 1, 2010 6:00 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)