WebDesires

using htaccess to force either www or not in your url

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support
There is a mobile optimized version of this page, view AMP Version.

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:

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

 

Force your site to remove www

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

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

 

Need Some Help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request Support

Author: Dean Williams

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