Speed up website performance with mod_deflate compression via .htaccess

The loading time of a website gets a lot of attention these days, at least since google said, that the loading speed is a factor for the ranking position. With compression of text files (CSS, HTML, JS) you can speed up the performance easily.

If the site is hosted on an Apache 2 webserver, there is an easy to add compression to your existing files without modifying or wrapping them – the only requirement is that Apache is compiled with mod_deflate (which should be standard on quality webhosting providers).

Simply add the following snippet to your .htaccess file:

# BEGIN Gzip
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|iso|tar|bz2|sit|rar|png|jpg|gif|jpeg|flv|swf)$ no-gzip dont-vary
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.[0678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48, the above regex won’t work. You can use the following
# workaround (comment the above line and uncomment the below line) to get the desired effect:
# BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html


# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

</IfModule>
# END Gzip

The “BrowserMatch” should prevent compression for older Browsers (inluding Internet Explorer), because there are issues with javascript compression (but it seems, that only certain versions of IE 6 are concerned…)

4 thoughts on “Speed up website performance with mod_deflate compression via .htaccess

  1. Thomas Einwaller

    because we discussed the topic of browser compatibility some days ago I just wanted to mention that IE6 has some issues with caching compressed CSS and JavaScript files which may lead to strange behavior of your website/app. You might consider configuring mod_deflate so that it does not send compressed content to that browser (or finally dropping support for that old buggy beast 😉

  2. admin Post author

    thanks, tom – sometimes i just try to ignore that old bastard – i changed the code and added a browser rule which should prevent compression for old IE versions…

  3. Pingback: Affiliate & Webmaster Blog

  4. Yogesh

    I looked into this a bit futhrer, and it looks like both Apache 1.3 and 2.x have support for gzip out the box, although different methods are used for utilising it with each.Seeing as the official WordPress reason for removing the gzip option was so it’s handled by Apache, which is theoretically faster than PHP having to do it, it’s probably preferable to do this. I also noticed that the total file size of WordPress’s pages is smaller when Apache gzip is used rather than the internal WordPress (PHP) gzip method.A little Googling will find you Apache 1.3 instructions I’m sure. But for those using Apache 2.x like me, sticking this in a .htaccess in your site’s root should do the trick:AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/json application/x-httpd-php application/x-httpd-fastphp application/rss+xml application/atom_xml application/x-httpd-erubyHeader append Vary Accept-EncodingIf it doesn’t work, your host needs to install the deflate module (usually enabled by default). Ask them and they should be able to do it.That will not only gzip your WordPress pages, but also the CSS, XML, and JavaScript files none of which are gzipped when the old WordPress function is used.You can add any MIME type to the collection above, but do bear in mind that you *don’t* want to be gzipping stuff like images, Flash files, etc, as they’re inherently already compressed; indeed, gzipping these will slow you down. I suggest just sticking to those above.

Leave a Reply

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