Gzip

From Seobility Wiki
Jump to: navigation, search

What is gzip?

Gzip is a file format and data compression tool commonly used to reduce the size of data transferred between a server and client when a website is accessed. It was originally a tool written to replace the Unix 'compress' utility, but has since become a common standard on the web for data compression.

Part of the compress utility in Unix relied on the LZW algorithm, which was patented. Gzip was developed as a free alternative; it is based on the Deflate algorithm, which is a lossless data compression format standardized in 1996. Certain gzip decompression implementations support streaming algorithms, which makes it well-suited for compressing files to be sent across the internet.

How gzip works

Gzip compression must be supported by both the client and the server in order to work. When a request is made to a server to access a file, the server will check within the HTTP header whether gzip is supported or not by the client. Gzip is effectively supported by all major browsers, so this is not typically an issue.

The server will then generate the page as usual, but instead of sending it directly to the client, it will run the resulting data through a gzip-compressed stream. This is sent to the client, which will uncompress the stream and render the resulting page in the browser.

Implementation

There are various ways that gzip compression can be implemented in a web project. These all pre-suppose that the server software supports gzip. As gzip is a well-established and widely-used compression technique, it is very typically supported by commonly used web software stacks, such as LAMP and WAMP.

Note that the file types most commonly compressed with gzip are HTML, CSS, JS, and other plain text files. Images and other multimedia files are not typically compressed using gzip, as these file types often use their own compression techniques already to reduce file sizes. It also can only compress files hosted on your server, so third-party scripts hosted elsewhere cannot be gzip-compressed.

Implementation using the .htaccess file

For those running on Apache servers, it is possible to enable gzip using a .htaccess file. This offers a fine-grain level of control over exactly which file types are compressed. An example looks like below:

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
</IfModule>

There are many different file types that may be compressed in this way, and which files you choose to add will depend on the structure of the website in question.

Activation via PHP

If you do not have access to the server a website is running on, it is possible to enable gzip compression via PHP instead. Note that the server must still support gzip compression in order for this to work.

There are 2 ways to compress a file using gzip within PHP. If you have access to the php.ini file, which contains PHP configuration settings, you can add this line to enable compressed files:

ini_set("zlib.output_compression", 1);

It is also possible to enable this on an individual file in PHP. Adding the following line of code to the top of a PHP file will compress that particular file:

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

Note that this will conflict with the php.ini setting, so both cannot be used at the same time.

Implementation using a CMS plugin

If you are not a developer or are not otherwise comfortable using code to enable compressed files, many popular CMS software support plugins that enable you to compress files without having to use any code.

For example, WordPress plugins such as Enable Gzip Compression, WP Fastest Cache, and WP-Optimize all provide options for enabling gzip compression to compress your files. The server must still support gzip, but as long as it does, turning it on is typically as simple as enabling it using a checkbox in the plug-in settings.

What advantages does gzip provide for search engine optimization?

While gzip compression does not directly influence SEO, it can decrease loading times, which are an important ranking factor. Serving compressed files can also help improve the user experience, as less data must be transferred from server to client when a file is compressed. This results in less data usage in addition to faster loading times for the end-user, which improves the user experience and helps reduce bounce rates.

Gzip is a very well-established compression technique, and it is used by some of the largest websites on the internet. Websites such as Facebook and Google have reported a file size reduction of as much as 77%. When Facebook enabled gzip compression on its homepage in 2009, it improved average page load speeds by 414%.

These are significant file size reduction and page load speed increases that should not be ignored. Tools such as Google PageSpeed will often recommend enabling gzip compression if it is not already. There are typically very few disadvantages to enabling gzip, and those are often outweighed by the advantages. Having a faster website that uses less data benefits all types of visitors, whether roaming mobile users or people at home browsing from desktop PCs.

Related links

Similar articles

About the author
Seobility S
The Seobility Wiki team consists of seasoned SEOs, digital marketing professionals, and business experts with combined hands-on experience in SEO, online marketing and web development. All our articles went through a multi-level editorial process to provide you with the best possible quality and truly helpful information. Learn more about the people behind the Seobility Wiki.