using htaccess to force either www or not in your url

One of the biggest mistakes when setting up a website is not deciding whether or not to use www in the URL to your website, almost all servers are set-up to automatically link the www. subdomain to your main domain because www. is widely used by many web masters. This presents a problem though because by default any website that is set-up is now duplicated on both www.mysite.com and mysite.com.

This presents a big problem because people who navigate directly to your site may do either and if the site loads for both URLs without a redirect that user can browse the site, share it on social media, link to it from their websites and etc. Google then picks up these two URLs and indexes them causing duplicate content which will harm your Google rankings considerably over time.

The first thing you need to do is decide whether or not you are going to use www.mysite.com or mysite.com, there are pro’s and con’s for both practices, we would recommend not using www. as your domain will look cleaner in the address bar, especially if you decided to go https in the future, a visitor will see your domain name instantly after the green https reassuring them that they are on the right domain.

However this is completely up to you and from experiments performed by many SEO experts Google does not seem to prefer either method.


Force your site to use www

All you need to do is add the following code into your htaccess file:

[text]RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1[/text]


Force your site to remove www

All you need to do is add the following code into your htaccess file:

[text]RewriteEngine On
# Force NO www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L][/text]

Author: Dean Williams

Professional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.

Leave a Reply

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