Tim TrottTim TrottAlways hope, never expect

Improve Website Speed by Enabling Compression

By , Sunday 22nd December 2013 in Web Design

Google has long stated website speed will impact search ranking, but how do you go about improving your website speed?

There are two ways that you can improve your website speed by enabling compression for your generated content.

The first method works for static content, such as HTML and CSS, while the second only works on pages served generated with PHP.

htaccess GZip Compression

The simplest method for enabling GZip compression uses the Apache module mod_gzip which is available on most hosting platforms. Mod_gzip allows you to use the Gzip compression method to create a significant reduction in the volume of web page content served over the HTTP protocol.

If mod_gzip has not been installed on your server, and you have access to install modules, you can use this tutorial to install mod_gzip.

Once installed, enabling GZip compression is as easy as adding a few lines to the .htaccess file.

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
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/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

If mod_gzip is not available, you can use the alternative mod_deflate which generally comes along with the apache package.

To enable mod_deflate simple add these lines to your .htaccess file.

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript

GZip Compression with PHP

PHP can easily compress generated content before sending it to the browser and works in the same way as I described a few years ago when I wrote about compressing JavaScript and CSS files.

The preferred method for enabling compression in PHP is to set the zlib.output_compression variable in php.ini

zlib.output_compression"0"PHP_INI_ALLAvailable since PHP 4.0.5.
zlib.output_compression_level"-1"PHP_INI_ALLAvailable since PHP 4.3.0.
zlib.output_handler""PHP_INI_ALLAvailable since PHP 4.3.0.

If you cant update your php.ini configuration file, you could try using ini_set php function.

ini_set("zlib.output_compression", "On");

Another option you can use is to use PHP's output buffering functions.

function print_gzipped_page()
    if( headers_sent() )
        $encoding = false;
    else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
        $encoding = 'x-gzip';
    else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
        $encoding = 'gzip';
        $encoding = false;
        $contents = ob_get_contents();
        header('Content-Encoding: '.$encoding);
        $size = strlen($contents);
        $contents = gzcompress($contents, 9);
        $contents = substr($contents, 0, $size);
// Do your PHP awesomeness here
echo 'Hello World';
// finally, call this function to send the compressed page

Using either one of these methods should result in faster website download speed, but you still need to have an optimised high performance website to begin with. If your page takes 10 seconds to generate, all the GZipping in the world isn't going to improve site performance.

Once implemented, you should test your GZip compression to ensure that it working correctly. There are numerous sites that can do this for you.

My website and its content are free to use without the clutter of adverts, tracking cookies, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

About the Author

Tim Trott

Tim is a professional software engineer, designer, photographer and astronomer from the United Kingdom. You can follow him on Twitter to get the latest updates.