Configuring .htaccess for Performance

Caching is useful for files on web server that rarely change. Images and other static content can be cached to avoid re-requesting them on each page load and hence, performance can be improved.

In this tutorial you’ll learn how to do file caching using Apache .htaccess file.

Enabling .htaccess on Ubuntu server

First step is to check that .htaccess is enabled on your linux server. If it is not, you can enable it here:

a2enmod headers

Example .htaccess for performance

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)(\.gz)?$">
Header set Expires "Thu, 31 Dec 2015 10:00:00 GMT"
Header unset ETag
FileETag None

<FilesMatch “\.(js|css|jpg|png|gif)$”>
SetOutputFilter DEFLATE
ETags are a mechanism to determine the origin of the content on the server. Disabling them not only makes the server work faster, but also allows the browser to rely on the Cache-Control headers.

For image files and pdf documents, we have set the cache to expire on “Thu, 31 Dec 2015”.

If you are on Apache 2.x, enable mod_deflate and compress all the js/css files. You can compress images as well.