htaccess code to redirect non www to www website

Many time by mistake we create websites using non www URL’S but after sometime while working on SEO part receive update from SEO people to change the website code from non www to www and its very frustrating part to make changes in all pages.

To avoid such boring work simply create .htaccess file under the website document root path i.e /home/user-name/public_html ( its for cPanel control panel user, you can ask your hosting provider to provide you document root path) and insert the following code.

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

Note : Make sure that you have changed the domain name in above code from your-domain.com to your original domain name.

About Anant 376 Articles
Senior technical writer