HostOnNet Blog

Redirect yourdomain.com to www.yourdomain.com

By default most sites allow yourdomain.com and www.yourdomain.com

It is better to select one of the url you want and stay with it instead of using both URLs for same web site.

It is better to use URL with www as it have some advantage when you have a large web site. For example, if you have multiple sub domains, and use main web site with out “www”, all your visitors to sub domain have to send cookie set by your main domain. This create unwanted problems, if all the sub domains are not under your control, it can even result in security issue. Even if you control all the sub domains, there is more chance that a hacker can gain access to get and able to read all cookie set by your main domain.

To redirect yourdomain.com to www.yourdomain.com, create an .htaccess file with following content.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

Posted in Apache