Posted: 26th November 2013

Author: Ric

Tagged: Tutorials

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, however, 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"
</IfModule>

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
</IfModule>

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.

Blog posts written by former QWeb employees are not necessarily an accurate indication of the current opinions of QWeb Ltd and the information provided in tutorials might be biased or subjective, or might become out of date.

Discuss this post

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 and you won't be added to any mailing list and your details won't be shared with any third party.