Webhosting Blog

Forcing a www to a domain


Forcing a domain to follow to redirect to www is a very simple task.

Eg. http://mydomain.com should redirect to http://www.mydomain.com.

The question would be rather why one should force using www for a domain. A very simple and straight answer is for search engine optimization!
Search engines like Google and others may penalize for having duplicate contents and hence dropping you in rankings in the search engine. In order to avoid this from happening, we can force the domain to use www. We can use the .htacces file for this task. It is important for one to modify the .htaccess file in the document root of your domain to prevent duplicate content in search engines.

1) Use following steps in the document root of your domain.

[root@server~]# cd /home/username/public_html

2) Open the .htacess file with any of your favorite editors

[root@server~]# vi .htaccess

Insert the following code in file.

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

Save the file.

3) Assign appropriate ownership to the file.

This will redirect all requests to the non-www version of your site to the www version using 301.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s