Some times you need to redirect all non-www requests to www for your site, this will be useful to avoid duplicate content, as the search engines may crawl tow copies of the same page, one with www and the other without, to get ride of this problem, you can use 301 redirect to tell search engines that the non-www page is the same as www one.
Making this type of redirect is easy as a piece of cake, just use the following lines in your .htaccess file, where website.com is your domain name.
RewriteEngine On RewriteCond %{HTTP_HOST} ^website.com$ RewriteRule (.*) http://www.website.com/$1 [R=301,L]
If you want a generic solution regardless of the domain name, you can use the following lines:
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This will work for any domain.
You can create the .htaccess file using a good text editor like TextPad, UltraEdit and Microsoft WordPad. You cannot use Microsoft NotePad. An .htaccess file can contain a single or multiple lines depending upon the functions you want to include in it. You will need to use an FTP (File Transfer Protocol) program to upload the file. Also you must to upload the file in “ASCII” mode. Some FTP programs use “BINARY” as the default to upload files. If that’s the case you must change it to “ASCII”. Upload the file to the directory you want it to reside in. when you look at the files in your website’s directory you might not see it as your web server’s software is probably hiding it.
Rewrite none-WWW to WWW
Redirects example.com to www.example.com