Scott Forsyth's Blog

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

.

  • Scott Forsyth

Hosting Needs

Training and Dev Labs

Redirecting Non-www to Domain Equivalent

A question came up on a blog post of mine on how to redirect a domain name that doesn’t have www in it while retaining the original domain name.  Basically, to have a generic redirect to add on a www if it’s not already there.

For example, how could you have something.com always redirect to www.something.com where something could be any of a number of domain names that you manage.

This is fully possible with URL Rewrite.  Here’s what the config looks like.  If you want a full walkthrough using the setup wizard then refer to my previous post for the details.


<rule name="non-www to www" enabled="true" stopProcessing="true">
<match url=".*" />

<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />

</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>

This rule watches for all 2 level domains and redirects to the same domain name and tacks on the www to the beginning.  So something.domain.com won’t be redirected, but domain.com will.

Alternately, if visitors will come in through either http or https, you can ensure that this retains the protocol with 2 rules, one for each:

<rule name="non-www to www http" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="non-www to www https" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>

Posted: Jan 05 2012, 10:00 AM by OWScott | with 21 comment(s)
Filed under: , ,

Comments

Jon Sagara said:

Hi Scott,

How would you generalize this to work with both http and https?

# January 5, 2012 12:00 PM

OWScott said:

Hi Jon,

Good question.  I just updated the blog post to include an example that achieves that.  Since there isn't a protocol variable with http or https then it takes two rules to achieve this cleanly.

# January 5, 2012 12:10 PM

savart said:

Is it required to add a rewrite? what happens if you leave an "out of the box" configuration, won't www.site.com and site.com type in a url both still work?

Thanks for your help.

# January 17, 2012 9:50 AM

OWScott said:

Hi Savart,

Yes, it's fine to leave the default configuration and support both URLs.  However for search engine optimization (SEO) it's beneficial to have a single URL, so this rule is for SEO reasons and not for visitor traffic.

# January 17, 2012 9:56 AM

Savart said:

I was under the impression that for SEO redirects were bad? Because it looks like you are trying to use different URLs to drive traffic, any thoughts on that?

# January 17, 2012 10:38 AM

OWScott said:

I won't claim to be a SEO expert, and it's always changing, but a limited number of redirects aren't bad at all. In fact they help with your page rank because you'll only have 1 site that grows in popularity rather than diluting that across a few names.  You may be right that buying a lot of domain names and pointing them to the same location will eventually have a negative impact, although, again, I won't claim to be an expert on that.

Here's a good link to get started.  The first paragraph jumps right into that: www.stephanspencer.com/redirects-and-seo-best-practice

# January 17, 2012 10:53 AM

Musa Ozcan said:

I think it useful but i couldn't find what section in the web.config i should add this code?

# January 28, 2012 9:51 AM

OWScott said:

Hi Musa.  Good point, it's not clear in the blog.  What I sometimes do is to create a dummy rule with the url set as something bogus.  That will create a placeholder for you.

To do it manually, the location is

<configuration>

 <system.webServer>

   <rewrite>

     <rules>

       {individual rules like the example above}

     </rules>

   <rewrite>

 <system.webServer>

</configuration>

# January 28, 2012 10:31 AM

Lkt said:

Also, using the rewrite to redirect non-www link to it's  www equivalent is useful when the pages have certificates associated with it. The browser will throw a certificate error to the end user if the certificate is for www.example.com and user is on example.com

# February 21, 2012 11:23 AM

OWScott said:

@Lkt.  Good point, that does help with not only SEO but also the single URL for the sake of the certificate.  As long as the redirect is handled pre-https.  It also means that this rule should occur *before* a http to https rule so that you ensure that the www is in place before the certificate is requested.

# February 21, 2012 12:03 PM

Anas said:

i tried to redirect sandiegolimotime.com to www.sandiegolimotime.com but it shows me an errer ???

# March 19, 2012 10:48 PM

OWScott said:

@Anas, what error message do you receive?  At the moment when I test it I don't see the redirect so I assume that you turned it off again since it's not working yet.

# March 20, 2012 3:42 PM

Randy said:

Scott,

Thanks for publishing this.

My impression is that having a status code 301 with the redirect is important for the SEO aspect.  I only say this because the SEO stuff I have read specifies creating a 301 redirect.  Is there a way to include this in your rule?

Thank You,

Randy

# March 23, 2012 1:00 PM

Randy said:

I just checked the headers on my redirected page and it shows a 301 redirect, so it sounds like nothing else is required.

# March 24, 2012 9:34 AM

OWScott said:

Hi Randy,

You're right on both parts.  The search engines prefer the 301, and the rule also defaults to a 301 if it's not explicitly set.

# March 24, 2012 10:34 AM

Randy said:

Got it -- thanks again Scott!

# March 26, 2012 1:04 PM

OWScott said:

Hi Lasersightpro,

Great, glad that helped! Thanks for the update and for the tips for other readers.

Scott

# June 20, 2012 9:48 AM

Phil said:

I found the condition for matching only works for single suffix .com type references.  It fails to match with .com.au so could be extended to read

^[^\.]+\.[^\.]+\.[^\.]+$

# August 4, 2012 6:41 PM

OWScott said:

Thanks Phil.  Good point.  I live too much in a .com type world and didn't account for that in this rule.  That's good to keep in mind to handle the extra level in the URL.

# August 7, 2012 10:00 AM

Jim Bacon said:

I have a mix of domains so I am trying a condition that redirects when the url does not begin www.

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

# September 11, 2012 10:16 AM

OWScott said:

@Jim.  That should do the trick.

# September 11, 2012 11:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)