Using mod_deflate For Compression

Using mod_deflate For Compression

It is always a good idea to compress the text content being served to your web site visitors. Not compressing content wastes bandwidth and slows down your visitors experience to your web site. All modern browsers currently support compression, and you can configure it within your Apache server config file, or through an .htaccess file for a specific application. This will significantly speed up the transfer time of all requested text based files making your web site load faster.

First, you need to ensure that you’re loading the mod_deflate module that ships with Apache. It is generally loaded by default on most installations; however, I have encountered it not being loaded, and needed to modify the LoadModule directive.

LoadModule deflate_module libexec/apache22/mod_deflate.so

Second, you’ll need to configure the mod_deflate settings. I generally include this in an .htaccess file if I don’t have access to the actual server config. This is one I always try to configure on the actual httpd.conf file, as it is considered a requirement by myself for all of my projects; however, in a hosted environment you may or may not have access to that file. So for such a reason, I have included it as though I’m putting it into my .htaccess file. The following example will compress all content except images (or more specifically except those with the named extensions).

<IfModule mod_deflate.c>
    # Enable Filter
    SetOutputFilter DEFLATE
    # Restrict Browsers w/Issues
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
    # Compress All Content Except Images
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    # Ensure Proxies Don't Modify Request
    Header append Vary User-Agent env=!dont-vary
</IfModule>

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

Leave a Reply

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