How to create .htaccess file?

After publishing m previous article many people are asking me that how should they create the .htaccess file in Windows OS. First of all let us make this thing clear that you will not be able to create .htaccess file on a Windows OS because in Windows you require to name a given file but in this case we are just creating  a file without name and just extension which will be illegal for a Windows OS.

You can overcome this problem by following the steps listed below:

  • First creating a htaccess.txt file in Windows OS
  • Open the file and add all the necessary code and commands to it and than save it.
  • Upload the file to your web server via FTP or via cPanel or any other panel provided by your webhost.
  • After uploading rename the file htaccess.txt to .htaccess in the interface provided by the cPanel of your webhost.

I hope you now know how to create .htaccess file.

How to perform redirection from www to non-www version of the domain or vice versa

Performing redirection fom non www to www or essential is a essential for SEO of a particular site because most of the search engines treat both www and non-www version differently and hence regard a single page as duplicate of other.

Search engines than put penalty on  duplicate content  in spite of the fact that your article is original and genuine.

This can easily be avoided by using .htaccess file in the root of your Website.

NOTE: This method is applicable only to those servers which allow mod rewrite in Apache(Linux) or IIS(Windows). Please confirm with your host.

Add following lines to redirect all www request to non-www version of your domain

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

Add following lines to redirect all www request to non-www version of your domain

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

In above two modification we use 301 redirect which means that a given page is permanently moved from this location thus telling search engines not to take this page as duplicate copy of other.