Web Design that taps into the haromny and vision of your dreams.

Improve Website Speed by Enabling Compression

By on in Coding

562 words, estimated reading time 3 minutes.

Search Engine Optimisation Series
  1. SEO - Search Engine Optimization
  2. A Guide to the Robots.txt Exclusion Protocol
  3. What are XML sitemaps?
  4. Using Google Webmaster Tools
  5. Getting Started with Google Analytics
  6. Getting Started Earning Money with Adsense
  7. Why Website Loading Times Are Vital - How to Improve Yours
  8. Improve Website Speed by Enabling Compression
  9. Timing PHP Script Execution
  10. 8 Excellent (and Free!) Search Engine Optimization Websites

An important factor for fast loading pages is serving compressed web pages, and Google has long stated website speed will impact search rankings. So 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.

Last updated on: Friday 8th September 2017



Have a question or suggestion? Please leave a comment to start the discussion.


Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.