How to enable Gzip compression and speed up your site

What is Gzip compression?

Gzip compression is a way of making your HTML, CSS, Javascript and other text files smaller than they are by compressing them. This can often save you around 50% to 70% of the file size which could help you save a lot of bandwidth and have more satisfied users.

How does Gzip compression work?

Developers from Google - Simply put, gzip compression works by finding similar strings within a text file, and replacing those strings temporarily to make the overall file size smaller. This form of compression is particularly well-suited for the web because HTML and CSS files usually contain plenty of repeated strings, such as whitespace, tags, and style definitions.

Enabling Gzip with .htaccess on Apache webservers

You can enable Gzip compression by adding a bit of code to the .htaccess file on your webhost/server.

Add the following code to your .htaccess file to enable Gzip compression:

  
  
AddOutputFilterByType DEFLATE text/plain  
AddOutputFilterByType DEFLATE text/html  
AddOutputFilterByType DEFLATE text/xml  
AddOutputFilterByType DEFLATE text/css  
AddOutputFilterByType DEFLATE application/xml  
AddOutputFilterByType DEFLATE application/xhtml+xml  
AddOutputFilterByType DEFLATE application/rss+xml  
AddOutputFilterByType DEFLATE application/javascript  
AddOutputFilterByType DEFLATE application/x-javascript  
  

Note: If you are not familiar with the .htaccess file please read this excellant article on feedthebot.com: How to use the .htaccess file

Enabling Gzip NGINX webservers

  
gzip on;  
gzip\_comp\_level 2;  
gzip\_http\_version 1.0;  
gzip_proxied any;  
gzip\_min\_length 1100;  
gzip_buffers 16 8k;  
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;  
gzip_disable "MSIE [1-6].(?!.*SV1)";  
gzip_vary on;  

Note: credit for this piece of code should go to Patrick Sexton from feedthebot.com

Enabling Gzip with PHP

It is also possible to enable Gzip using PHP. This can be useful if you are not able to access or use an .htaccess file on your webhost/server.

The following code snippet should be used at the top of your PHP pages. (Don't forget to place it between the PHP tags)

  
 ob_start('ob_gzhandler');

Resources

Some resources with more information on Gzip compression:

Recent Posts

Social

" data-via="wesleysmits93" data-lang="en">>Tweet View WesleySmits's profile on slideshare

Follow me on Google+!

Join my Community!