John Hawthorn

gzip compression in apache through MultiViews

Today I wanted to enable gzip compression on this website, but I’m unable to use mod_gzip or mod_deflate. Fortunately, being static content, I was able to use content negotiation with Apache’s MultiViews. First step was to add the following to my .htaccess

Options +MultiViews
AddEncoding x-gzip .gz

After that I needed to gzip compress all of my files, with the suffix .en.gz and append .en to all files. Now, requests to index.html will be served index.html.en.gz for any browser claiming to support gzip compressio. Other browsers will be served the original file from index.html.en. As a side effect, index (without extension) will also redirect to one of those two.

This has a couple (very small) advantages. First, it’s faster than compressing on the fly. You’re also going to be able to get a better compression ratio, since you can use gzip -9 (about 3% better) or 7z (about another 3%). Realistically, I’d have used mod_gzip if It was available.

When researching this solution, I came across a couple posts from people who are under the impression that nowadays it’s fine to serve gzip encoded pages to any browser. Please don’t do that. It makes uzbl users sad.

On a side note, Firefox allows you to change what you send in the Accept-Encoding header. It’s in about:config under network.http.accept-encoding, default is, predictably, “gzip,deflate”. I’m going to be as interesting as I can to the web servers I meet and accept “gzip,deflate,inflate”.