htaccess Snippets to Improve Website’s Performance

The power of .htaccess file of a website in terms of optimizing the performance of a website often goes unnoticed. It can be utilized to speed up a website and reduce its loading time by a significant margin. You need not to be a rocket scientist in order to do that. A few simple fixes here and there can help you in meeting the purpose pretty easily. Here are 10 .htaccess snippets to improve website’s performance.

1. Purge the URL from www

It is advised that the URL must be stripped of the www part for SEO purposes. This can be done with the below mentioned .htaccess snippet that will strip the www part and redirect your site visitors to the non www URL:

2. Make URLs future proof and SEO friendly

Search engines deride sites with complex URL structure, such as those with addresses like ‘index.php?product_id=’. One should avoid creating such a site structure that is not SEO friendly. Instead, the focus must be on creating cleaner URLs that are liked by the search engines. Such URL structure is easier to manage than the former. One may add as many sub folders to the site structure as he or she may want.

Following code snippet can be used to prevent Apache from identifying files and deciding how to handle them on the basis of their extension with the following code:

<Files magic>
ForceType application/x-httpd-php5

3. Prevent Hotlinking

Hotlinking is the process of utilization of images and other files of a site by another website. In this way, the bandwidth of the former is used illegally by the latter, thereby resulting in slowing down of its speed. Hotlinkers must be stopped in their tracks in order to prevent them from using the crucial bandwidth of a given site. Here is .htaccess code to prevent hotlinking:

4. Use rel=”canonical” tag

If your website is having pages with similar content or want to prevent web scrapers from getting undue advantage of your content, you can make use of the rel=”canonical” tag for this purpose. This will make Google and other search engines know that the particular page or document is the original one and it should not index any other page with similar content. For images and PDF documents, one can use the tag on the HTML page or the download page of the same.

Here is how to use the tag:

5. Redirect feeds from WordPress to Feedburner

WordPress blog owners can redirect all feeds they are using on their blogs to their feedburner feeds using this code:

6. For mobile content crawling use the Vary header

In case you are altering the page HTML instead of responsive design while using the same URLs for mobile access, you can send the vary header for this purpose.

Header append Vary User-Agent

7. Force download of chosen file types Website owners who want to force download of chosen file types instead of letting the browser decide the type of file can make use of the following code:

8. Better site speed with improved caching Site speed has a tremendous bearing on both user experience and search engine ranks. Increase the site speed with improved caching with this code:

9. Custom 404 page Set up a custom 404 error page to bring lost visitors back to track for improving their overall experience while visiting your site.

ErrorDocument 404 /cool404.html

# Can also do the same for other errors…
ErrorDocument 500 /cool500.html

10. Compress static data to reduce page weight

If you send compressed data to the visitors, the client at their end will decompress the data and help in reduce bandwidth usage on both ends.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s