Force www in URL using htaccess

UPDATED 03/01/2017 - Fixed a bug in the code, sorry!

Some people still prefer to use www in their websites, however using www. is no longer a standard for websites and was invented when the internet first started as an indicator that you was requesting a website, which was especially useful as most of the worlds population was unfamiliar with the internet and this allowed for adverts or publications to print web addresses and the reader would be able to identify that the text was a URL. However when you use www you are in fact accessing a sub-domain of the website, personally I would not ever recommend the use of www. as it clutters up the address bar, is completely outdated standard and hides away the beauty of your unique domain name.   To be honest though it is not as bad as allowing both www.mywebsite.com and mywebsite.com - which presents a serious site duplication problem for search engines! So ultimately you will need to decide whether you want to force the www. on your website or not, and this article will show you how to force www from your urls - personally I think urls like far cleaner without the www and technically if you use www your site is a sub domain which is nonsensical. If you want to force removal of the www please see my other article: Redirect and remove www from URL with htaccess

Force use of www in your URLs:

Here is the code snippet:

# Force www

RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]

RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Why do either?

It is important that you force one method or the other, as allowing both causes many issues such as duplicate content. A few important benefits include:

  • It will avoid duplicate content problems with search engines such as Google.
  • It will avoid the possibility of split page ranking and/or missing rankings.
  • It is nicer and more consistent for you and your visitors.
Facebooktwitterredditpinterestlinkedinmail
Author: Dean WilliamsI'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

Post Tags:
,
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments