Redirect non-www URLs to www URLs using .htaccess

In terms of SEO reasons, it doesn’t matter if you use www.yourdomain.com or yourdomain.com. But people link to both www and non-www versions of the domain so in order to avoid duplicate content generated by search engines (two versions of the same page are seen as coming from two different sites), you can “force” your visitors to use either the www or the non-www version of your domain. If your website is doing well and is already crawled by search engines spiders, we would recommend to check which version is more widely used and make that option permanent.

Make sure that you backup the .htaccess file before you proceed. The following code makes a 301 redirect to the www version of your website.

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

From now on, when someone accesses http://yourdomain.com he will be redirected to http://www.yourdomain.com. The same goes for search engines.

P.S. Don’t forget to also select Preferred domain setting from Google Webmaster Tools.

301 redirect using htaccess explanation

Do you want to build a flawless WordPress site? 

Related Blogs

Web developer
Symfony-PHP-Developer_02
Quick question?