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 happier 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.

How to enable GZIP Compression with the .htaccess file on Apache Webservers

You can enable GZIP by simply adding a bit of code to the .htaccess file on your webhost / server. The code below will enable it on your server.

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

<ifmodule mod_deflate.c=""> 
  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 
</ifmodule> 

How to enable GZIP Compression on NGINX Webservers?

By adding the following code to your config file you can enable GZIP.

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; 

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)

<?php ob_start('ob_gzhandler'); ?>

How to test if GZIP is working for your site?

It’s very easy to test if GZIP is correctly working on your site by simply checking the URL in any of the numerous tools online. One example of this is this tool by Small SEO Tools.

Resources

Some resources with more information on GZIP:

Tools

LEAVE A REPLY

Your email address will not be published. Required fields are marked *