Reducing the traffic between your web server and your web browsers, by compressing your web pages means you will improve your site speed, so that you will make it more user friendly. You can do this by implementing gzip compression.
What is gzip compression?
Gzip is a file format and a software application. It was created as a free software replacement for the compress program used in early Unix systems. It is used for file compression and decompression. It is based on the deflate algorithm: a combination between LZ77 and Huffman coding.
How does gzip work?
It finds similar strings within a file and then compresses them out, so it can reduce the file size of your pages and style sheets up to 70%. The more matching strings you have in a file, the smaller your files will be after compression. The good news is it is supported on a majority of browsers, including the mobile ones and it is ideal for html.
Why is it important?
It is an easy way to optimize your site’s speed and save bandwidth. If you think that you need a modern browser to enjoy web content, you also need great web speed, so you use gzip encoding. If it strikes home, Google and Yahoo use it.
Now why are we using gzip?
When a browser request a file, the server searches for it and then it gives the browser the response code with its size. If it gives a large file, let’s say of about 100KB, then the browser will have to load for a while. We all know how annoying that is.
Although you get the file, it’s not efficient. This happens due to the size of the file and of the redundant HTML code. There can be many repetitive words throughout the document, so you can gzip it.
This way you save on bandwidth and download time. Your user will gain more satisfaction due to the fast loading page.
How does it function?
The browser sends a header telling the server it accepts compressed content (gzip and deflate) : Accept – Encoding: gzip, deflate.
The server sends a respond if the content is actually compressed: Content – Encoding: gzip.
Now, the choice of accepting encoding: gzip, deflate belongs to the browser. What you need to do, however, is to configure your server so that it will return gzip content if the browser accepts it.
How to configure your server for gzip compression
For IIS, enable compression in the settings.
In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file:
Apache has two compression options:
- mod_deflate is easier to set up and is standard.
- mod_gzip seems more powerful: you can pre-compress content.
If you can’t change your .htaccess file, you can use PHP to return compressed content. Give your HTML file a .php extension and add this code to the top:
<?php if (substr_count($_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’)) ob_start(“ob_gzhandler”); else ob_start(); ?>
Did it work?
Now you only need to check if your compression works:
- Online: Use the online gzip test to check whether your page is compressed.
- In your browser: Use Web Developer Toolbar > Information > View Document Size, to see whether the page is compressed.
- View the headers: Use Live HTTP Headers to examine the response. Look for a line that says “Content-encoding: gzip”.
So this is how you can optimize your site’s speed by using gzip compression. Hope it works!