Bandwidth control is an important task to be followed when your traffic is increasing. With limited monthly bandwidth hosting, your site may run out of bandwidth and thus result in increase in down time. Hence it is very much advisable to compress your websites response with GZip and then serve it to client. Compressing output can significantly improve your websites performance by reducing the size sometimes by upto 80%!

So how can you enable GZip compression and compress your websites output? Well there are several ways to achieve this. Listed below are some very useful tricks to enable compression.

GZip using mod_gzip, mod_deflate and htaccess

Apache server supports server level GZip compression with the help of module mod_gzip and mod_deflate. You can use this module and enable GZip compression for your website using htaccess file. First you have to check whether your hosting provider has enabled mod_gzip or mod_deflate module or not? To check this, you can use php_info() function of PHP which prints all the information about server and modules.

You can enable compression for text and html by adding following lines in your htaccess file.

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

# Or, compress certain file types by extension:

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

You can compress all type of content (image, css, js etc) using mod_deflate. Copy following code in htaccess to do this.

<Location />
    SetOutputFilter DEFLATE
      SetEnvIfNoCase Request_URI  \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI  \
        \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
</Location>

Also, you can add following code in your htaccess file and enable compression using mod_gzip.

<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.*
</IfModule>

This technique only works if mod_gzip or mod_deflate modules are loaded in Apache. In our case, these modules were not there and our hosting provider refused to load it as we were using a shared hosting. So following can be another way of enabling compression.

GZip using PHP ob_start() method

If your hosting provider does not support mod_gzip module, ob_start() method can be used to enable compression in PHP file. For this you need to copy following line in top of the PHP file. You may want to add this line in start of the header file that gets included in every php.

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

Above code will check whether your browser supports gzip, if yes, then it send ob_gzhandler method as handle to ob_start method which buffers the output. Thus output is compressed using ob_gzhandler. Only problem with this method is that you have to manually edit all PHP files or should have a header.php file that gets included in all files. There are still ways to achieve this without touching your PHP files. Read following trick.

GZip using php_value directive in htaccess

php_value directive can be used to append/prepend any PHP files in the request of change the output handler. First we will see how we can prepend a PHP file and achieve this. Copy the PHP code that we saw in above example in a file called gzip-enable.php. Now copy following lines in your htaccess file. Thus you need not to modify any of your PHP files can can prepend a PHP file with ob_start() method to all the files.


<FilesMatch "\.(txt|html|htm|php)">
    ForceType application/x-httpd-php

    php_value auto_prepend_file /the/full/path/gzip-enable.php

</FilesMatch>

But what if you don’t want to prepend a PHP file? Still there is a way to specify default output handler using htaccess. Use following line in your htaccess file to tell your apache to register ob_gzhandler handler function as output handler.

php_value output_handler ob_gzhandler

Compress CSS using htaccess and php_value

CSS Stylesheet files occupy significant size in overall webpage size. It is hence advisable to compress these files before sending them to client. This significantly improve the performance of a webpage. For compressing CSS files, we will first create a PHP file gzip-css.php with following code.

<?php

   // initialize ob_gzhandler function to send and compress data
   ob_start ("ob_gzhandler");

   // send the requisite header information and character set
   header ("content-type: text/css; charset: UTF-8");

   // check cached credentials and reprocess accordingly
   header ("cache-control: must-revalidate");

   // set variable for duration of cached content
   $offset = 60 * 60;

   // set variable specifying format of expiration header
   $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";

   // send cache expiration header to the client broswer
   header ($expire);
?>

Now add following lines in your htaccess file to enable compression for CSS files.

<FilesMatch "\.(css)">
    ForceType application/x-httpd-php
 
    php_value auto_prepend_file "/the/full/path/of/this/file/gzip-css.php"
 
</FilesMatch>

Whenever a http request for .css will come to a server, the type of css will get converted to application/x-httpd-php, thus making them parsed using PHP. Then a file gzip-css.php will be prepend to this request which in turns compress the output using ob_start (“ob_gzhandler”) method.

After adding above code in your files, you can check the gzip compression is enabled or not by using the following URL:http://www.whatsmyip.org/http_compression/

We use Gzip compression on our blog and make some great savings, see for yourself: Umbrella Web Studio Gzip Test

With Web Hosting from Umbrella Host your server is enabled from the outset allowing you to benefit from the tips above, theres also a plus side that web hosting packages from Umbrella Host come with ample bandwidth so you wont be hitting your limits anytime soon. Visit Umbrella Host and compare the web hosting packages for yourself.

Tags: , ,

Post discussion

4 Responses to “Optimize Your Site With GZIP Compression”

  • amit sharma October 5, 2011

    That`s a really nice article. But can you please give me some advice on how to Gzip a Blogger blog?

  • Adolfo Swantner August 6, 2011

    Hey! That’s a really great post. I’m very certain Ill suggest it to my co-workers.If you submit extra posts please e-mail them to me.

  • free ipad August 6, 2011

    Yo man and gracias , for this supper great content. I work on websites as designer and have found your site very inspiring and have came across from search on ask . Will be sure to read trough . Gracias , best regards. ^_*

Leave a comment

Download Project Worksheet

Our project worksheet which goes into more details about your requirements, helping us to better understand your specific developement and design needs.
Project Worksheet

Socialize with us

We want to socialize with you, whether you are a customer, someone interested in our services or just someone that wants to hear what we have to say.

TechnoratiStumbleuponFlickrFacebookRedditDiggBloggerDeliciousTwitter

Newsletter

Our newsletter is awesome. With up to date news on our latest projects and the latest in web development. Subscribe and get the latest updates.

Login to your Umbrella Project

Login to Umbrella Web Studio and manage your project(s)