Published Tuesday 26th November 2013

How to speed up your site with .htaccess

Websites today are all about high quality images and streaming media. With large screens becoming more affordable and the increasing resolutions of mobile and tablet devices, designers are going all out to give their pages that wow factor. The result is that modern web pages consist of larger files, which means a lot of data transfer to your visitors before they get to see anything. If you receive a lot of traffic or your target audience don't all have fancy high speed connections, this can be a big problem.

There are plenty of optimisation techniques you can implement, from resizing images and minifying your CSS to implementing a CDN like Amazon CloudFront. There are also two things you can do in seconds, on almost any Apache server, which can noticeably speed up your page load times.

First of all, we recommend that you take advantage of cache control with Apache's mod_expires module. If you really think about it, even the most dynamic of websites have a certain amount of content that's unlikely to change very often. Images, javascripts and stylesheets can usually be given an expiry time of at least 1 week.

Paste the following code into your .htaccess file for a generic configuration and adjust/extend for your website:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 weeks"
    ExpiresByType image/jpeg "access 1 weeks"
    ExpiresByType image/gif "access 1 weeks"
    ExpiresByType image/png "access 1 weeks"
    ExpiresByType text/css "access 1 weeks"
    ExpiresByType application/x-javascript "access 1 weeks"
    ExpiresByType application/javascript "access 1 weeks"
    ExpiresByType text/javascript "access 1 weeks"

Secondly, you should consider enabling gZip compression using Apache's mod_deflate module. It's a slightly controversial technology and, although it's been around for years, I've only recently started to recommend using it.

The Deflate module compresses all outgoing data using gZip. Web browsers today all have the ability to decompress this data for rendering and, because a web page consists almost entirely of image and text files, which are often very compressible formats, simply enabling gZip compression can hugely decrease the amount of data transferred, which minimises bandwidth requirements. For the best performance you should really compress your images manually and leave gZip to compress only text files.

Paste this in to your .htaccess file to enable text file compression:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript

The downside of gZip compression is the addition processing required to compress and decompress the data, so depending on who your visitors are likely to be, it may have a negative effect on performance. For example, if one visitor has a top of the range PC but is connecting through an old 56K dial-up line, it makes sense to compress the transferred data and allow their shiny new PC to do the work, but another visitor with a fancy fibre connection might be struggling with old hardware, and expecting that hardware to decompress the data, that you could have sent uncompressed in less than a second, doesn't seem logical.

Today, most desktop users have both solid hardware and high speed Internet connections, so, for really large pages, compression is the obvious choice. Even for small pages, there's a growing trend in mobile browsing where the hardware is often much better than the connection, so, again, compression should be used. Be careful, though, if your website is designed primarily for an audience less likely to keep up with the times.

For more optimisation tips, check out Google's PageSpeed Insights tool.

Photo of Ric


Ric is a senior web and game programmer with nearly 30 years industry experience and countless programming languages in his skillset. He's worked for and with a number of design and development agencies, and is the proprietor of QWeb Ltd. Ric is also a Linux server technician and an advocate of free, open-source technologies. He can be found on Mastodon where he often posts about the projects he's working on both for and outside of QWeb Ltd, or you can follow and support his indie game project on Kofi. Ric also maintains our Github page of useful scripts.

Blog posts are written by individuals and do not necessarily depict the opinions or beliefs of QWeb Ltd or its current employees. Any information provided here might be biased or subjective, and might become out of date.

Discuss this post

Nobody has commented yet.

Leave a comment

Your email address is used to notify you of new comments to this thread, and also to pull your Gravatar image. Your name, email address, and message are stored as encrypted text. You won't be added to any mailing list, and your details won't be shared with any third party.

This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.