seo: how to enable compression on your web server

There are many techniques in SEO that are quite easy to implement. Some of them also have other advantages in addition to improving your SEO “score” with search engines. Enabling compression on your web pages is one of them. This allows you to save bandwidth, download time as well as improve the page load times.

Page speed is an important factor in SEO. Faster loading web pages keep the users happy as well as tend to do better in search engine rankings. What compression helps you with is the page speed of your web pages.

There are primarily two different types of compressions that are used: gzip and deflate Gzip compression is the most used and usually the default implementation used by most web sites, but you can also use deflate with good effect.

HTML which is the web markup language is a pretty verbose language. Most times it tends to have as much layout and template “code” and tags as much as the actual displayed content of the web page. In order to display a page correctly, the browser needs to download the entire code and page content along with all the related files such as Javascript files, CSS files, and images etc.

The total page size or the payload size is the sum of all these files. So, Keeping the total size to a minimum will reduce the total download time and increase the page load times. We know that compression is a good way to make large files smaller, that helps to transfer of files over the wire faster as well.

In order to compress the file and display it correctly to the user, there are several little things that need to happen. Both the web server and web client/web browser need to understand that the file is to be compressed. When the browser makes a request for the web page, it will also send the list of encodings or encoded content types that it can “understand” and properly handle from the server. This is conveyed using the header field in the request named Accept-Encoding. 

Depending on the value of the Accept-Encoding header in the request received from the client, the server then decides on the format in which the content should be sent to the client. If the server decides to serve the content in the  compressed format, then it will send along the type and format of the content again using a HTTP header field in the response: Content-Encoding.

You can configure your web server to serve compressed content as and when the browser supports it. Most modern web servers support compression either by using built-in routines or by using third party modules. Let us see the differences between the two widely used compression techniques to find which one should ideally be used.

Deflate vs Gzip

deflate is a very simple compression technique and it is pretty fast as well. This makes it a good choice when speed of compression and ease of implementation is a factor. Despite some of the conveniences of deflate, gzip is much more widely used than deflate.

Gzip usually have a better compression ratio than deflate making it ideal when size is a major factor. Gzip also allows you to pre-compress the files, thus saving a little bit of time and processing load on the server. These compressed files can then be cached for even faster downloads.

In Apache Web Server, you can enable compression in the .htaccess file. Apache can use either of the compression methods: deflate or gzip. You specify the compression method using the corresponding apache module: mod_gzip or mod_deflate. You can enable compression based on the file type or by the file extension.

Using Gzip

Insert the following code into your .htaccess file located in the topmost folder of the web site.

<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 mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*

Most of the above code is self explanatory. It basically turns on the compression in the first line of code. It then enables compression for several file types: htm, html, txt, css, js, php and pl. It also enables it for files with mime types text and javascript. It disables the compression of image files as image files are already compressed.

Using Deflate

The apache code for enabling deflate is pretty similar to the above one for Gzip. Below is an example of specifying the compression on a per mime type basis.

<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
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml

No matter how you enable compression, you need to verify that the compression feature is working. You can do using any of the web page analysis software or online websites such as Page Speed Services, Pingdom or Check Gzip compression. etc.

In addition to enabling the compression feature, there are several things that you can do to make sure that the web compression works optimally to give you the best performance.