GZIP Compression Techniques for Better Page Speeds

“Page speed” and “site speed” have become buzz phrases in the SEO industry since Google started showing site performance data in Webmaster Tools 1 in December 2009.

In April 2010 Google stated that site speed would be a part of the ranking algorithm. Although it is worth noting that Matt Cutts reminded webmasters that site speed is only a minor ranking factor, and that relevancy is far more important.

Like us, our users place a lot of value in speed – that’s why we’ve decided to take site speed into account in our search rankings. 2

In 2009, Google performed tests to determine the impact of site page speed on user behaviour. As part of this experiment Google intentionally slowed down the rendering of search engine results pages.

Slowing down the search results page by 100 to 400 milliseconds has a measurable impact on the number of searches per user of -0.2% to -0.6%. That’s 0.2% to 0.6% fewer searches for changes under half a second! 3

This therefore raises the question, “How important is site page speed as a ranking factor?”

How Important is Site Page Speed

Matt Cutts reminded webmasters that relevancy is more important than page speed, in fact site page speed is a minor ranking factor at this time, and is just one of over 200 signals used in Google’s ranking algorithm. This could change in future, but page speed should not be seen as a major ranking factor at this time.

Tools of the Trade

It is worth noting that there is a lot of opinion on Google’s Page Speed tool in terms of how useful it is for website owners, and how the information can easily be misinterpreted. The same applies to Yahoo’s YSlow tool, as some of the recommendations are simply not feasible for smaller website owners. Therefore, I recommend looking at speed data for your site from a variety of sources, and avoid making drastic changes to your site unless you fully understand the implications of such changes.

  • Pingdom.com – Free tool, useful graph feature highlighting problems on pages and slow loading scripts
  • Loads.in – Free tool to test site speed in multiple locations
  • Site-Perf.com – Free tool for testing with specific browsers and other useful options

In isolation, the above tools have limited scope, but running your site URLs through a number of tools will highlight any problem pages that are taking too long to render. There are other premium tools such as CloudShare for much more detailed testing and monitoring of performance.

The purpose of this article is not to enter the debate as to how good or bad Google’s Page Speed tool is, but rather to provide a robust method for speeding up your website – whether you choose to use Google Page Speed or not.

User-experience is an important consideration that goes beyond simply SEO benefits, especially when considering Google’s studies on page load time. Therefore, it is good practice to consider site performance as long as page speed improvements are implemented correctly, and never at the expense of providing good-quality, relevant content.

The old rule “test and test again” comes to mind when tweaking your sites performance.

GZIP Content for Improved Page Speed

I’ve been experimenting with a number of different techniques to speed up page load time. My aim was to increase rendering time for mobile and tablet devices rather than as a specific SEO consideration. The tweak that had the biggest impact in my experiments was GZIP compression of content. Any small SEO benefit from a fast website is welcome but not a large factor in my experiments, whereas a fast loading site is pleasing to users.

Before implementing any of the following techniques, I recommend running your pages through Google Page Speed, Yahoo YSlow and some of the tools mentioned above, as well as this GZIP testing tool, (to check whether GZIP is already implemented and give you page size details as shown below). Keep a spreadsheet of performance data to act as a benchmark. This is useful for seeing what changes have an impact on your page load times.

A Page with no GZIP compression

No GZIP Compression

A Page with GZIP compression

GZIP Compression Yes

As you can see in the images above, GZIP compression drastically reduces the size of a page if it is enabled. In the lower image, a reduction of 65% has been achieved.

The following tips will not work for every website, and it is assumed you are using PHP on an Apache server, with the ability to create .htaccess files. It is possible to implement compression on other servers, but this is beyond the scope of this article.

GZIP Content at Server-Level

If you have access to your server and can add (or edit) an .htaccess file in the root folder (I.e. located here www.example.com/.htaccess), you can simply add the following line of code to GZIP your site content:

<IfModule mod_gzip.c>  
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html|htm|css|js|php|xml)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text\.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.* </IfModule>

WARNING: Be extremely careful when editing your .htaccess file. Always backup your .htaccess file and be prepared to roll back if your site doesn’t perform as expected.

GZIP Content on Shared Hosting Using PHP

Some shared hosting services don’t have the GZIP option enabled on the server which means the above technique will have no effect on site performance. If that is the case and you aren’t seeing improvements in page load times from your earlier benchmarks, there is another method that will work, however it has the disadvantage of being CPU intensive, and is somewhat awkward. However, you may decide to try it on larger external files that you call on every page (I.e. CSS or JavaScript file) initially to see what impact it has on page load times.

There are in fact several ways to implement this method, but I have found the following method to work the best. If you are familiar with using PHP includes, you could automatically call the PHP into your pages.

At the beginning of your pages (must be PHP pages), you add the following code:

<?php
  ob_start( 'ob_gzhandler' );
?><!DOCTYPE html>
<html>
...
</html>

To be more thorough, the above code can be extended to ensure the GZIP module is switched off after use. To do this, you need to add some code to the top and bottom of your pages (this can also apply to JavaScript and CSS files).

<?php if(extension_loaded('zlib')) {
  ob_start('ob_gzhandler'); }
?><!DOCTYPE html>
<html>
...
</html><?php if(extension_loaded('zlib')) {
  ob_end_flush(); }
?>

You can add the above code to external JavaScript and CSS files as well, but you need to change the name of these files as follows (and remember to update your pages to refer to the .css.php and .js.php file extensions):

  • http://www.example.com/css/stylesheet.css.php
  • http://www.example.com/js/javascript.js.php

Below is an example of how this would look for an external CSS file:

<?php if(extension_loaded('zlib')) {
  ob_start('ob_gzhandler'); }
?>
body {
  font-family:helvetica,arial,sans-serif;
  font-size:13px
}
article,aside,figure,footer,header,
hgroup,nav,section {
  display:block
}
...
<?php if(extension_loaded('zlib')) {
  ob_end_flush(); }
?>

By using one of the above methods you should be able to compress content on your site and benefit from faster page load times, but as mentioned before, thoroughly test any server-level changes that you make, as servers can be setup to behave differently. I have tested the PHP GZIP method in various browsers with no bugs or issues, but again, be sure to perform your own tests on a variety of browsers and on different platforms.