Content encoding and compression optimisation

One of the best things about modern browsers is their ability to encode and compress static and dynamic files using gzip to allow for faster load times and to save bandwidth. The tricky part is telling the browser and server to send, receive the compressed files.

The basic idea behind the compression starts with the browser sending an “Accept-Encoding: gzip, deflate” header. This then requests the server to send the compressed file and the server returns with the response of “Content-Encoding: gzip” to tell the browser it has accepted the compression method before the transaction takes place.

If this header is not returned then either the browser does not support the encoding method (older browser) or the browser did not send the correct response (configuration issue). Enabling compression in apache is pretty simple, you can enable it by adding the below code to your htaccess file.

# compress all text & html:
AddOutputFilterByType DEFLATE text/html text/plain text/xml

# Or, compress certain file types by extension:
<Files *.html>
SetOutputFilter DEFLATE
</Files>

This uses the deflate method for html (static) or PHP dynamic pages. However if you are on a server that does not allow you to edit the htaccess file than you can use the PHP code below in the header of the page to implement the gzip equivalent.

<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’)) ob_start(”ob_gzhandler”); else ob_start(); ?>

To go further on this, you can also optimise your CSS files to reduce white space. There are several tools online that can help you with this task. One such tool is www.cssdrive.com. This is a nice little online tool that will reduce the code to content ratio of your CSS by removing not only whitespace but unwanted comments as well to get the best compression possible. This can also be done if you have Adobe Dreamweaver by applying source formatting, however the only downside to the Dreamweaver option is it does not support the truncation or removal of comments.

Adam Wood
SEO Programmer

  • Twitter
  • Sphinn
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • Tumblr
  • FriendFeed
  • Technorati
  • Reddit
  • Simpy
  • StumbleUpon
  • Slashdot
  • LinkedIn
  • Netvibes
  • Propeller

Leave a Reply