.htaccess Redirect from Non-WWW to WWW

21 Jul
2009

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

If you can access your website both with www and without then you are effectively serving duplicate content. I am sure Google are sensible enough to filter this out but some people feel like it could be an issue so my recommendation is better safe than sorry.

Place the following code in your .htaccess file to automatically force www. on the front of your URLs:


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

You might not need the “RewriteEngine On” – check if it already exists in your .htaccess

There are a lot of other solutions using .htaccess but this particular solution does not need editing (i.e. your URL is not in the code) so you can use it in a distributed project or many websites without having to customise it.

Bookmark and Share

1 Response to .htaccess Redirect from Non-WWW to WWW

Avatar

Accessing Websites Without a Subdomain

August 25th, 2009 at 6:40 am

[...] you can access your site without the www, make sure that the traffic is forwarded to one or the other to prevent potential duplicate content issues with [...]

Comment Form

top