Force users to use only site with www subdomain

If you want to use only www-enabled website, you can use .htaccess to make it possible. Why it's so important?

  1. SEO. Google will see your www.example.com and example.com as two different websites and mar them as duplicate content.
  2. Session, cookies and some other componens only work for single domain as security restriction. You will not have session variables in www.example.com that was set in example.com
So how to force only www usage of website? It's simple
 
Open plain text editor (I suggest using Notepad++). Save new document as .htaccess. NOTE make sure it's not .htaccess.txt
Than copy this code to your newly created file
 
Options +FollowSymLinks
RewriteBase /

RewriteEngine On

It will enable your URL rewriting. Below that text paste following lines

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
It will redirect anyone that will come to your site to www version of site.
Comments:
Replaying to
Leave comment
Alternative: DNS | Reply Cancel
You can also use DNS A record to use www. as alias.
Yandex.Metrica