How To Redirect Your-Domain.com to www.Your-Domain.com

In the context of the directories at the server, http://your-domain.com is the root folder. The www part in http://www.your-domain.com is a subdirectory or a subdomain which usually redirects to the root folder. The “www” prefix has no meaning in the way the main Web site is shown. The “www” prefix is simply one choice for a Web site’s host name. It is mostly used because a host name for the web-server is often ‘www’.
If you are using an Apache server to host your site, then the best way to redirect visitors from http://domain.com to http://www.domain.com is by using a .htaccess file.
.htaccess file is the one that has the configuration settings for the particular directory it is placed in. If you are using your hosting provider’s file manager menu then in many cases you may not be able to see the .htaccess file in your root menu. In that case you can download FileZilla, an excellent open-source FTP client. After locating your .htaccess file and downloading it, it is a good idea to make a copy(with original settings) of it first as a backup.
Now to redirect from non-www to www enter the following code in the top of the .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain.com [NC]
RewriteRule ^(.*)$ http://www.your-domain.com/$1 [L,R=301]
Just type in your domain name instead of “your-domain”.
To do the vice-versa, ie to redirect from www to non-wwwenter the following code instead in the top of the .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^your-domain.com\.com
RewriteRule (.*) your-domain.com/$1 [R=301,L]
Now, If you are using WordPress as your site’s blog software then make sure, that in the settings menu, you change the “WordPress Address (URL)” and “Blog Address (URL)” accordingly. Otherwise you will get a “This site was moved to this location” error!