You may be able to access some websites using both www and without www as part of the URL. For example http://yourdomain.com or http://www.yourdomain.com. It is upto the webmaster to decide which one to use. But there are some significance of using only one version. The use of “www” as a sub domain name (prefix) is not required by any technical or policy standard but is a long-standing practice of naming Internet hosts.
In this article we discuss about the significance of forcing the browser or search engine bot or any client to use only a specific version of the URL and how to achieve this by configuring your .htaccess file. We will use the Search Engine Friendly 301 redirection method for web page redirection.
www/non-www canonical issue- Significance of Forcing users to use Either www or no www
If you allow both version of URL to access your site there is a possibility of duplicate content get indexed in search engine index. It is commonly referred as www/non-www canonical issue. This results in duplicate content in search engines. This is a serious issue.
Google Webmaster tools allows you to choose the preferred version of your domain. Either www or no www. But other search engines doesn’t not provide any such mechanism to choose the preferred domain.
Redirect the site from without www to with www
This will force the users including the search bots to use the “www” version of your domain. You can add redirect instruction in .htaccess file to achieve this. This is used to redirect all users including the search bots who access the site without the “www” prefix.
If you are not familiar with .htaccess file read the following articles:
- .htaccess Tutorial -A guide and .htaccess tricks and tips
- How to create and use .htaccess file? Common usage and risks
- Password Protecting website Pages and Directories using .htaccess
- Block IP Address of The Spammer Using .htaccess to Block Spam
For example if you want to redirect all requests which are pointing to http://yourdomain.com to http://www.yourdomain.com
Add the following line to the .htaccess file
1 2 3 4 5 | # mod_rewrite in use Options +FollowSymlinks RewriteEngine On RewriteCond %{http_host} ^yourdomain.com [NC] RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L] |
In the above code replace “yourdomain.com” with your website domain name.
Redirect the site from with www to without www
This will force he users to use the “no www” version of your domain name. This will redirect all requests which are pointing to http://www.yourdomain.com to http://yourdomain.com
You can add the following instructions to the .htacess file.
1 2 3 | RewriteEngine On RewriteCond %{HTTP_HOST} !^www.yourdomain.com$ [NC] RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301] |
In the above code replace “yourdomain.com” with your website domain name.
Before you go, subscribe to get latest technology articles right in your mailbox!.