GZipping Web Content for Fun and Performance

I’ve known about the benefits that gzipping the content on my sites would give me for quite some time (files are zipped up as they travel to your users, making them faster to download and leaving you with a smaller bandwidth bill), but I never got around to enabling it because, well, I’m lazy.

It’s really easy to set up things on your server to automatically gzip all sensible files though, and most of the code you need is very easily found in the Apache 2 documentation. All you have to do is copy and paste the following into your .htaccess file, and all your files (except images) will end up being gzipped for you. Awesome.

<IfModule mod_deflate.c>
  # Insert filter
  SetOutputFilter DEFLATE

  # Netscape 4.x has some problems...
  BrowserMatch ^Mozilla/4 gzip-only-text/html

  # Netscape 4.06-4.08 have some more problems
  BrowserMatch ^Mozilla/4\.0[678] 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 to get the desired effect:
  BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

  # Don't compress images
  SetEnvIfNoCase Request_URI \
  \.(?:gif|jpe?g|png)$ no-gzip dont-vary

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

In my tests I’ve seen a 70-80% drop in over the wire weight for those gzipped files. Not too shabby at all.

My advice? Whack that code somewhere in your .htaccess file and watch your bandwidth usage plummet like a stone.

If you enjoyed reading this and would like other people to read it as well, please add it to del.icio.us, digg or furl.

If you really enjoyed what you just read, why not buy yourself something from Amazon? You get something nice for yourself, and I get a little bit of commission to pay for servers and the like. Everyone's a winner!

comments (2) | write a comment | permalink | View blog reactions


TrackBack URL for this entry: http://www.workingwith.me.uk/cgi-bin/type/mt-tb.cgi/785


  1. by Jim O'Donnell on February 20, 2008 12:12 PM

    Thanks for the tip! I’ve built mod_deflate on our server and set up gzip using these rules in httpd.conf for the Maritime Museum.

    Funny thing is, it’s working for text/html content but not compressing javascript or css files. I can’t see anything that would handle those mime types differently. Is it compressing css and js files for you?

  2. by Jim O'Donnell on February 20, 2008 01:23 PM

    Oops, ignore my previous comment - I was being immensely stupid and had forgotten to clear the uncompressed CSS and JS files from the firefox cache when I checked the NMM site.

Write a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

other relevant pages

about wwm

workingwith.me.uk is a resource for web developers created by Neil Crosby, a web developer who lives and works in London, England. More about the site.

Neil Crosby now blogs at The Code Train and also runs NeilCrosby.com, The Ten Word Review and Everything is Rubbish.