Optimise Your Apache Server to Get the Best Service for You and Your Users

Date Published: 26/02/2009 00:08

Reducing file sizes and removing unnecessary HTTP requests made to your server can not only reduce the tole on your bandwidth but also improve the user experience when using your site. Sites laden with with large high resolution images and complex flash animations are becoming common place on the world wide web as connection speeds continue to increase. Whilst this bring many benefits it also takes any attension away from developers optimising their sites to provide the best possible service.

Apache Software Foundation Logo

Web professionals have a tendancy to assume that their users are using a 10Mb connection and will be able to download their 6MB flash intro video in mere seconds. Sadly this is not the case. Whether its because the user is running downloads, is in an area with bad signal quality or just has not yet upgraded to a fast modern connection there are various reasons why a person may be browsing the internet on a slow connection. The last thing as developers we want to do to that person is exclude them just because their connection is not as fast as it could be. That is why I have written these few quick easy tips on how to streamline your site using apaches built in features to ensure that users access your site as quickly and easily as possible. These tips are all based on guidelines present by yahoo and implementing them all will give you a Y!Slow score of A, making it apparent that you care about users with slower connections

Optimising with the .htaccess file

There are a few simple lines which you can add to your .htaccess file on your site which will set apache up to use these bandwidth saving features. If you do not have a .htaccess file at the moment or you are unsure of what it is simply create a text file called ".htaccess" (note no file extension) in the root of your site and apache should detect it automatically. If this is not the case contact your system administrator for further details.

Header unset ETag
FileETag None

The above 2 lines when added to your .htaccess file will stop apache sending ETags out with assets from your site. The ETag is used to track changes to a file on the server so they can be compared to the cache of the users web browser. Removing the ETags forces the browser to compare date changed values instead which is a much more accurate method of tracking changes to content.

<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Thu, 15 Apr 2012 20:00:00 GMT"

The above lines dictate to the server to send out expiry headers with any files with the extensions dictated in the first line. By setting those expiry headers far in the future it stops the users browser from request these assets again if they are in the cache and therefore stops unneccessary HTTP requests. As a rule of thumb I would recommend not applying this rule to js and css files as these are most likely to change and could damage the user experience if they are changed but not re-downloaded. If you make any big changes to your site I would recommend disabling these few lines (by putting a # at the start of each line) so that your changes can be properly circulated until browser caches expire.

SetOutPutFilter DEFLATE

This last .htaccess option uses the apache module DEFLATE to compress all text based output. Using this option means all files with a text mime-type are compressed using gzip and can be reduced in size by up to 80%. This option is vitally important, especially for very text heavy sites as it massively decreases the amount which a user has to download when accessing your site.

All the options stated above are all entirely server side changes and will not effect the user in any way other than quicker download times. All internet browsers are compatible with these modifications.

Minify your JavaScript on the Fly

Adding functionality to your site using JavaScript can be great but when you have finished you often end up with massive JavaScript files which are full of needless comments and whitespace. To combat this a man named Douglas Crockford wrote a small app called jsmin which strips out those unneccessary elements without damaging the file. Crockfords app is now available in many forms such as a windows .exe programs or python, ruby, perl scripts which allow you to minify your code prior to run time. However, we are going to use the PHP version of jsmin to minify our JavaScript on the fly meaning we can keep our scripts in their true form without the user seeing them.

First you will need to download the PHP version of jsmin from Douglas Crockfords website (http://www.crockford.com/javascript/jsmin.html). Then you will need to create a small PHP script which will be referenced by the page as a JavaScript file when infact its a PHP script masquerading as a JavaScript file. In that file you will need to first send a header indicating the file is JavaScript, then include the jsmin library before finally echoing out the minified JavaScript. Here is an example:

// Tell the browser the file contains JavaScript
header("Content-Type: text/javascript");
// Include the jsmin library
include($_SERVER["DOCUMENT_ROOT"] . "/libs/jsmin-1.1.1.php");
// Use the minify function of the JSMin static class to output a minified version of page_script_full.js
echo JSMin::minify(file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/scripts/page_script_full.js"));

Now simply point your page(s) to use this new script instead of page_script_full.js and you will make a massive saving on transfer times.


All of the tips I have outlined above are quick and easy to implement so there is not really an excuse why not to use them. They will benefit both you and your users so why not take a few minutes to implement them, even if you can not do them all one or two of them will help.


Sorry comments are currently disabled for maintenence

5 Most Recent Articles

Manually Triggering Events in ASP.NET from JavaScript

A quick guide for ASP.NET developers on how to manually trigger ASP.NET events from JavaScript.

Advanced Use of MySQL Stored Procedures

An article for users of MySQL databases describing how they can use advanced stored procedures to improve efficiently in their applications.

Using MySQL Stored Procedures and Extending MySQLi in PHP

A guide for LAMP developers to using stored procedures in MySQL and extending the MySQLi class.

Reading and Writing to Excel Spreadsheets in Python

An introduction to using the xlwt and xlrd modules for python to interact with Microsoft Excel spreadsheets.

Interact with the Web Using Python and the HTTP Library

This is an introduction to making HTTP requests from a python script/application using httplib.