.htaccess Essential Tricks For Blog/Website Performance – Guide

by Hiroshi on February 14, 2009

in htaccess, Tricks, Useful Scripts


.htaccess file is a magic file. You can change the way your website works by using it. I have been reading a lot about .htaccess files. Perticularly .htaccess is very useful in shared web hosting environment where the users have no direct access to their servers or they want to implement some restrictions/security to their files. I will include here some very essential and useful .htaccess tricks. I have used some of these tricks and more to secure my websites according to my web server’s environment and to optimize speed/performance of my websites. Websites with heavy traffic, Digg traffic, Stumble upon or slashdot traffic will need to be optimized via many ways and .htaccess can help a lot.

Data Compression

Use this code to compress data at web server. Data compression can minimize resources and bandwidth you consume while your website browsing.

# data compression
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/javascript

GZip Data Compression

Following is GZIP Compression code. More effective. You can specify which files to compress.

# gzip compression
<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl|jpg|png|gif)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

Preserve Bandwidth

Preserve bandwidth using this method. This is also a compression method.

# preserve bandwidth
<ifmodule mod_php4.c>
 php_value zlib.output_compression 16386

Cache Control

Cache is very very useful. By using following code in .htaccess you can enable cache of your files. When enabling cache you will see significant increase in website load time. You will be really amazed to see heavy pages and images loading very fast.

# Begin Cache Control
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800

Secure .htaccess file

Deny .htaccess file to all intruders. You can specify more files which you want to protect by seperating file names by | sign. .htaccess file can have potential information which must be secured from all outside intruders.

# secure htaccess file
<Files .htaccess>
 order allow,deny
 deny from all

Secure Directories

By default your web server’s indexes are on. That means the directory with no index file in it will display all the files in it. Do you want the files in your wp-admin, wp-includes, images, uploads, themes, plugins folders to be seen? It is really not a good idea to show your online folder files. By using following code you will enable protection for all online directories with no index file and will not display files in them.

# No directory listing across domain
Options -Indexes

Secure Hidden Files at Server

Hidden files can be server configuration files, log files and other server reserved files which should be protected from hackers as well.

# Disallow hidden files
RedirectMatch 403 /\..*$

Stop Hotlinking

Some webmasters will just copy the path of images and other media from your website and use that in their own website to display your content consuming your bandwidth. This method is called as hotlinking. Protect hotlinking by using the following code. sorry.gif in the following example is the alternative file which will be displayed when someone will try and steal your content.

# stop hotlinking and serve alternate content
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?yourservernamehere.com/
RewriteRule \.(gif|jpg)$ https://www.yourservernamehere.com/sorry.gif [R,L]

Secure Contact Forms

Secure your contact form from bots and automated softwares which will email you spam. Change ‘contact.php’ page name to your contact page name.

# secure contact forms
RewriteCond %{HTTP_REFERER} !^https://www.yourservernamehere.com/.*$ [NC]
RewriteCond %{REQUEST_POST} .*contact.php$
RewriteRule .* - [F]

Redirect Error Pages

Have you ever encountered 500 error. Internal Server error. That’s really ugly black and while page. I would suggest you to create your own custom error pages and use them wisely to make users stay at your website even some error occurs. Use the following code to redirect the users to your custom error pages in case some error occurs.

<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 206 https://www.yourservernamehere.com/maintenance-mode.html
ErrorDocument 400 https://www.yourservernamehere.com/maintenance-mode.html
ErrorDocument 408 https://www.yourservernamehere.com/maintenance-mode.html
ErrorDocument 500 https://www.yourservernamehere.com/maintenance-mode.html
ErrorDocument 404 https://www.yourservernamehere.com/404.html

Disable ETags

Disabling Etags will also increase website speed a bit. Removing ETags is a very safe option.

# disable etags
FileETag none

Enable Simple Mod Rewrite

This is not a performance tip. This is how you enable rewriting at your server. I will write a guide about Mod Rewriting in next .htaccess post.

# enable basic rewriting
RewriteEngine on

Instruct Browser to Download Multimedia Files – Force Download

If you want to tell the browser to download certain file types then use the following code.

# instruct browser to download multimedia files
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .wmv
AddType application/octet-stream .mp3

In the end let m give you two beautiful resources about .htaccess.

Best Huge Resources About .htaccess Tricks

PerishablePress – .htaccess Tricks (BEST)

Online .htaccess Editor

Use this .htaccess editor to select and create rules easily.

More Resources

Following links will lead you to more detailed resources about .htaccess file customization.
.htaccess Caching Resources
How to Optimize Your Site With HTTP Caching
Speed-up Sites With .htaccess Caching
Data Compression
Compress PHP, CSS, JS & Optimize Website Performance
More Resources
corz.org .htaccess tips
Unix .htaccess Files

I am happy to share .htaccess useful tricks. I had searched much for .htaccess codes to optimize my website performance. My next target for .htaccess is Mod Rewrite. How we can Rewrite URLs. Like blogs do. They can change the URL in a friendly way and display post title rather than https://www.homepagename.com?post=some-digit

Allow me to give you all these codes in ready to use .htaccess file in downloadable form.
Just change ‘yourservernamehere’ in the file with your domain name and remove any part which you do not want.

Download .htaccess File

Here are some more examples. htaccess tricks cheat sheet

Happy Coding…

Related Posts

Previous post:

Next post: