Redirect your website users to your preferred URL, either WITH or WITHOUT the ‘www.’ prefix

If your website can be accessed with and without www then its considered having duplicate pages. If you have ever integrated the “facebook like” button on your website pages, try accessing the page with www and without www; you will notice different like counts displayed on the pages.

To prevent these duplicate pages on your website, you can redirect non-WWW traffic to WWW or vice-versa using mod_rewrite in your .htaccess file.

To redirect all users to access the site WITH the ‘www.’ prefix (http://mywebsite.com/… will be redirected to http://www.mywebsite.com/…)

Use the code below in your .htaccess file:


RewriteEngine On

RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]

RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]

To redirect all users to access the site WITHOUT the ‘www.’ prefix (http://www.mywebsite.com/… will be redirected to http://mywebsite.com/…)

Use the code below in your .htaccess file:


RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]

RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Leave a Comment

Back to top