Get in touch with me Your new website awaits.

Force WWW using an htaccess 301 redirect

From March 24th, 2007 | 48 Comments

Maybe it’s just me, but I get kind of annoyed when websites are accessible by both their non-www and www URL. If you attempt to go to my site by using http://trevorfitzgerald.com/, you’ll see that you’re automatically sent to http://www.trevorfitzgerald.com/ instead.

Why should you do this?

If you require people to login to your site, you probably give them a cookie so they don’t have to login each time. But that cookie is only good for one or the other URL, not both. So if somebody logs in using http://yoursite.com/ and comes back later using http://www.yoursite.com/, they’ll be forced to login again, even though a cookie already exists for them using the other version.

Also, forcing one URL variety over the other ensures that you won’t be double indexed or receive split page ranks in the search engines. While Google does a pretty good job on their end of preventing this, it’s possible that you could have two distinct page rank values for each version of the web page, even though they are 100% identical in content, but only differing in the presence of “www” in the URL.

How do you do it?

It’s simple really. Just add the following code to your .htaccess file in the highest folder of your web directory and yor visitors will be forced to use the www version.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^trevorfitzgerald.com [NC]
RewriteRule ^(.*)$ http://www.trevorfitzgerald.com/$1 [L,R=301]

48 Responses

  • March 24, 2007 at 2:08 pm

    Excellent suggestion. The only problem is, it won’t work for sub-domains….. i.e. MINE…. LoL

  • May 22, 2007 at 9:12 am

    thanks!

  • July 14, 2007 at 2:02 am

    Just Googled for this and came to this site. Thanks for the info.

    Subscribed to feed, stumbled ;)

  • July 24, 2007 at 1:44 am

    Wow, I was looking for this code and google redirected
    me to this great site.

    Thanks for the .htaccess code.

    Mohsin

  • August 11, 2007 at 11:47 pm

    Thanks for this! I reversed it though, to force a non-www dedirect.

  • L. Simon
    September 18, 2007 at 4:08 pm

    You can do the same for subdomains, assuming you have the ability to edit and apply the .htaccess file. Try:

    RewriteRule ^.*foo.bar.com(.*)$ http://www.foo.bar.com$1

    That may lose a forward slash in there, but tinker with it, and it will work Smile!

  • September 23, 2007 at 7:44 pm

    that helped a lot without having to restart the whole apache. thanks.

  • September 30, 2007 at 4:38 am

    I use Google Webmaster tools (sItemaps>>Preferred Domain)for this purpose…
    and my SE juice PR, backlinks remain remain for both domains ;

  • kenny
    October 12, 2007 at 8:59 pm

    Great fix.

    What do I do with url like

    http://bar.com/foo?

    it wom’t work.

    Thanks for help in advance.

  • Jeff
    February 10, 2008 at 2:56 am

    Great resource!

    I found your site on Freekat’s Knowledgebase…

    All of this talk about google is making me nauseous.

  • February 26, 2008 at 8:45 am

    Thanks!
    Just what I needed.

  • March 5, 2008 at 9:39 pm

    Thanks for the great, easy to follow advice. It worked like a charm!

  • Daz
    April 3, 2008 at 2:42 am

    The WWW. really is obsolete, I remove it completely…

    RewriteCond %{HTTP_HOST} ^trevorfitzgerald.com [NC]
    RewriteRule ^(.*)$ http://trevorfitzgerald.com/$1 [L,R=301]

    Check out http://no-www.org/

  • DavidK
    April 6, 2008 at 12:34 pm

    Daz, it’s not that obsolete, if someone types in the domain name minus .tld into their browser and hits CTRL+Enter their browser auto-adds www.{domain}.com.

  • Jesse Donat
    April 14, 2008 at 5:59 pm

    Yeah, I reversed this, I hate www. I never type it any more.

  • Jake
    April 15, 2008 at 3:52 pm

    Any thoughts on using https? When I use the above code it changes my https as well, so…

    https://mysite.com becomes http://www.mysite.com

    Making my ssl unusable

    Is there an adjustment in the code to take this into consideration. Want the rest of the site to be http://www.mysite.com

    Thanks

    Jake

  • RCM
    April 26, 2008 at 11:30 pm

    never mind i had two of these….i put it in the wrong file. thank you very much on the instructions

  • ne0
    May 3, 2008 at 6:02 am

    Thanks for help Smile!

  • Alastair Brunton
    May 22, 2008 at 4:29 pm

    Great thanks for the tip. I used it on http://www.simplyexcited.co.uk

  • Greg Guster
    August 29, 2008 at 12:55 pm

    Thanks, didn’t even realize this was a problem until a user let me know. Googled it, found this site, fixed the issue in about 80 seconds. You are the best. I used it on http://www.talkairline.com

  • Nani
    October 4, 2008 at 4:14 am

    Most hosting sites redirect everyting to your domain that is not an explicit subdomain. The use a wildcard like in our case: *.internani.com to send not only internani.com and http://www.internani.com but everything to our site. So to force everyone to go to http://www.internani.com we use a ! (=NOT) in the RewriteCond like this:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.internani.com [NC]
    RewriteRule ^(.*)$ http://www.internani.com/$1 [L,R=301]

  • James
    October 17, 2008 at 10:02 am

    Could someone explain the [NC] and [L,R=301] parts of the code for those of us who would like to learn what is going on in the back end of this code?

    I assume the 301 references redirects, but now sure what the L,R and NC are doing. Thanks.

  • Nani
    October 22, 2008 at 5:11 pm

    [NC] means No Case, or Not Case sensitive, so the value of HTTP_HOST will be compared with the string on the right side case insensitively. L stands for Last, if the RewriteRule applies it will be the last rule that gets executed, after which the proces leaves the .htaccess. R simply stands for Redirect. R=301 means Redirect and send a 301 Status code. 301 means that the Redirect is permanent.

  • Trevor Fitzgerald
    October 22, 2008 at 5:53 pm

    Nani,

    Thanks for the explanation and your previous tip! Smile!

  • Richard
    October 28, 2008 at 9:22 am

    Thanks im using this on my site Smile!

  • berita
    October 29, 2008 at 3:51 am

    Great thanks for the tip.

  • Nani
    November 1, 2008 at 11:52 am

    Thank you Trevor. My tip contained an error though, it should be $1, not just 1:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.internani.com [NC]
    RewriteRule ^(.*)$ http://www.internani.com/$1 [L,R]

    If you want to prevent hotlinking (people ’stealing’ your bandwidth by directly linking to your pictures and flash movies) you could add something like this after adding the redirect code.

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^.www.internani.com/.*$ [NC]
    RewriteRule ^(.+\.(gif|jpg|jpeg|bmp|png|swf))$ – [L,F]

  • Nani
    November 1, 2008 at 11:55 am

    Sorry Trevor, that was wrong, please change that anti-hotlinking code into this:

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^.*.internani.com/.*$ [NC]
    RewriteRule ^(.+\.(gif|jpg|jpeg|bmp|png|swf))$ – [L,F]

  • Trevor Fitzgerald
    November 2, 2008 at 2:01 pm

    Nani,

    Beautiful! I actually need that for a current project of mine. Perfect timing!

    Thanks Smile!

  • lubomir Herko
    January 5, 2009 at 12:08 pm

    nice one.. worked like a charm Smile!

  • Frank
    January 25, 2009 at 12:53 pm

    thx a lot great little workout!

  • James
    January 26, 2009 at 10:12 am

    Thanks Trevor, tried a couple of these and your seems to work a charm first time. I was having an issue with my SSL certificate appearing invalid at http://mantality.co.za and working fine at http://www.mantality.co.za – so this has sorted that out perfectly.

  • James H
    January 28, 2009 at 5:41 pm

    My boss registered several domain names to keep our competition from getting them. What is the best thing to do for these new domain names to prepare them for proper SEO use? I have heard that the older the domain is the more impact they have on SEO. Do I need to point them to one of our existing web sites while they age, or do some sort of 301 redirect thing? What would be the best “white hat’ process to do with these domains to help us out when we start to use them for web sites?

  • Loving You
    January 31, 2009 at 8:32 pm

    Exactly what I was looking for, thanks!

  • Rahul
    March 19, 2009 at 2:00 am

    found this post really helpful…thanks a lot..
    Searched it in google.

  • dan
    April 25, 2009 at 3:04 pm

    Actually, you may want to use this code just for looks or for redirecting when an SSL certificate is just for the www domain name. Having said that, I’d like to say that your initial statement about leaving a cookie is incorrect.

    You can easily set cross subdomain cookies simply by leaving out the explicit subdomain alias.

    setcookie(‘.domain.com’) <— this will work on www, abc, def, nothing, or anything

  • Josh
    May 21, 2009 at 11:18 am

    To those who think that no-www is better than with-www: http://www.web1marketing.com/blog/index.php/archives/whats-wrong-with-no-www/

  • Thanks
    July 8, 2009 at 4:36 pm

    Thanks!! Used on http://lakesidecontrols.ca

  • Najeeb
    July 24, 2009 at 4:17 am

    Thanks. It is really helpful.

  • jimmy
    July 25, 2009 at 8:27 am

    Great! lol-email.com is now http://www.lol-email.com
    Got that done in 2 mins thanks to you!

  • al
    August 10, 2009 at 9:38 pm

    Why don’t you have it go the other way instead, redirect http://www.domain.com to domain.com? This way it’s shorter… just wondering if there’s any reason or if it’s just a matter of preference.

    And if it really does matter, since like you said google is on top of things.

  • sagar
    August 11, 2009 at 9:49 am

    Great Help.Thanks dude

  • Ratnesh Dwivedi
    August 18, 2009 at 4:50 am

    Thanks for the help. it’s good for all developer who is looking for domain redirection.

  • seointexas
    September 10, 2009 at 12:20 am

    thanks brother, I work in web development, and it is a bit embarrassing, but I have gone years without this help. You are a true knight.

  • Rob
    September 28, 2009 at 4:48 am

    Nice one, i find the following works better

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

  • Bob Chapman
    October 27, 2009 at 5:36 pm

    When I have an option (not possible on all hosts), I always redirect to the non-www version. Think of it from a branding and public relations point of view. You want people to see your domain first, not the meaningless “www.”

    You do want to help people to remember your site and name, don’t you?

  • BennyZen
    November 5, 2009 at 4:34 pm

    Works like a charm^^
    thx mate

  • Bobby
    November 12, 2009 at 12:23 pm

    Thanks, worked perfectly!

Leave a Reply

Are you on Twitter? You can also comment via Twitter by clicking here.

Please enter your name, e-mail address, and comment. Thank you!
Twitter

Twitter Updates

  •  

You should follow me on Twitter here.

RSS

From The Blog

03Mar

Introducing Earthly WordPress Theme

Based on my first WordPress theme, Crispp, Earthly incorporates a color scheme designed for sites that require an eco-friendly or earthy theme. Features 100% Free Consistent styling across all major …

Continue Reading…

28Dec

YouTube Resizer jQuery Plugin

The jQuery YouTube Resizer plugin automatically resizes embedded videos to fill up the entire content space. It expands the video if the content area is larger and contracts if it is smaller. My first…

Continue Reading…