Redirecting www to non-www Using .htaccess
Posted on April 24th, 2009 by Gabriel HarperThe Apache Web server supports URL rewriting with the mod_rewrite engine. Placing custom rules in an .htaccess file lets you do all sorts of useful things to keep your URLs tidy. One really handy thing you can do for search engines and visitors is redirecting traffic from www to non-www version of your domain (and vice versa).
Some people prefer to use www.somesite.com, but some people prefer the shorter somesite.com. There isn’t really a right or wrong way to do it, but whatever you choose you can make sure all of your visitors get sent to the same place. With a few simple rules on the server you can choose from non-www to www, or redirecting from www to non-www.
If you already have a file named .htaccess on your Website you can add to it. If not, create one (yes, include the period at the beginning). Add either of the following rules and save. Replace yourdomain.com with your actual domain name.
Redirect www to non-www:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC] RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301] </IfModule>
Redirect non-www to www:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^yourdomain.com [NC] RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301] </IfModule>
Both of these rules send a full search engine friendly 301 HTTP redirect. They also preserve the entire URL (so yoursite.com/about redirects to www.yoursite.com/about).
Search engines can usually figure out which format is preferred, but since you’re showing the same page for www and non-www URLs it can’t hurt to be consistent about it.
May 5th, 2009 at 8:30 pm
Good reading
very similar to my post…
http://www.cashvolume.com/seo/canonicalize-your-domain/
I gotta start blogging again and catch up!
September 10th, 2009 at 3:41 pm
Thanks for this. I just added it to my htaccess on my strength-building fitness site and it worked perfectly.
November 22nd, 2009 at 12:51 am
Thank you Gabriel. This works perfectly for me.
December 30th, 2009 at 10:50 am
thanks man !! it worked perfectly a sec after i uploaded the new .htacces
. perfect
.
January 13th, 2010 at 8:51 pm
This is exactly what I needed for my blog! Thanks dude!
February 1st, 2010 at 3:55 am
Thank you so much for sharing this! I’m a bit hopeless with html but your instructons were really easy to follow. Very grateful – thank you!
February 4th, 2010 at 6:45 pm
nice one, used it on my rakeback site. cheers. www is so 1990’s!
February 9th, 2010 at 2:45 pm
Very handy. This post ranks pretty highly when I search for \redirect www to non-www\. Just what I needed.
February 9th, 2010 at 4:50 pm
Thanks so much I just changed it on my site – I think the non-www just looks better…
February 15th, 2010 at 5:48 am
Thanks for the valuable tips. The page rank of our home page divided due to non-www and www. Now I moved the site permanently to www. Thanks alot.
February 15th, 2010 at 11:43 am
Thanks!
It’s working for me!
March 3rd, 2010 at 1:06 am
Hi,
Earlier i was using www, now when i am tring to Redirect www to non-www: it is not working for me. Please help.
March 10th, 2010 at 2:22 pm
Thanks… exactly what i needed.. dont know why but my last code didnt work.. but this sure does!