Advice, tutorials and tips for beginner and experienced software/web application developers
Optimise Your Apache Server to Get the Best Service for You and Your UsersDate 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.
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
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.
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.
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.
// 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
An article for users of MySQL databases describing how they can use advanced stored procedures to improve efficiently in their applications.
A guide for LAMP developers to using stored procedures in MySQL and extending the MySQLi class.
An introduction to using the xlwt and xlrd modules for python to interact with Microsoft Excel spreadsheets.
This is an introduction to making HTTP requests from a python script/application using httplib.