10 most useful htaccess tricks

1.Redirect all traffic of www domain to without www-Most of the search engines(including Google) consider  www.yourdomain.com and yourdomain.com as a two different domain.So it necessary to redirect the traffic from one domain to another domain to avoid the penalty of duplicate content.Here is the htaccess code which will redirect all traffic from www.domain.com to domain.com.

You can also do the reverse of this process by using given codes.

 2.Enable gzip compression  –To speed up your website you need to compress it before sending the content to the visitor.Using htaccess you can do it easily for certain file types.

or you can use mod_deflate as another compression method .

3.Setting up browser caching- Caching files on visitors computer save the bandwidth of user and server .Caching provided a faster browser experience to the user.

 4.Redirect a URL to another URL-Redirect a URL to another URL.

5.Using a custom 404 page-You can redirect your visitor to your desired page when file not found error occurs.

 6.Passing request of a URL to different page-It will pass the request of one page to another page.The difference between fourth and this rule is that the address in visitor’s browser will not change but in fourth rule URL will change.

7.Disable directory traversal-To improve your website’s security you can turn off directory browsing by using this rule.

To turn it  on you can use this code.

 8.Change default index page-Generally index.php or index.html are treated as index page but you can change it with your desired page by using these codes on your .htaccess file.

 9.Prevent hotlinking-You can block other websites to use your images and other static file to save your bandwidth.

 10.Change default time zone-You can change the default time zone which is used by your PHP script .

A complete list of time zone can be found here.