How to Force www with htaccess File (and the Other Way Round)


Why force www with htaccess? When a website gets submitted to search engines such as Google and bing and the searchbots begin to crawl, there is a high chance that the search engine would interpret a page in two different ways:

Let’s take an ‘About us’ page for example:

Believe it or not although the content on these pages would be the exactly same, the search engines will see these URL’s as two different pages which causes the issue of duplicate content and could have a negative effect on your SEO.

However… never fear, here is a great little .htaccess script that forces (redirects) the www. at the beginning of the URL.


# Force www
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Alternatively, you may wish to force the non-www version of your site to display in your visitor’s browser.

#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

You can edit the .htaccess file using the File Manager in cPanel or via FTP.

To test this, when you type in your domain name with either www in front or not, it should display as you have set it in the .htaccess file.

Wheregoes is a free and very useful tool for tracking and testing the redirection paths of URLs, so perfect to test if your domain is redirecting as you wish.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *