Simple, static web pages still have their place alongside complex, dynamic, database-driven behemoths. Some web sites are small and simple enough for a collection of .html files to be the best method to store and serve them.

Now that most web browsers have the ability to accept gzip-compressed HTML, these files can be stored in a compressed format as well as the original uncompressed one. Modern browsers will be able to retrieve these smaller versions while less up-to-date browsers can still get the original, uncompressed files. This all happens automatically, so the people browsing your site won't need to do anything differently. This is good for two main reasons:

  1. Because the files requested are usually the smaller ones, the people browsing your web site can get them much quicker. This can really help out those on slower internet connections.
  2. The smaller transfer sizes help you out as well because the accumulated effort of many visitors will use significantly less of your bandwidth.

It's worth noting, however, that there are some drawbacks:

  1. You will be storing two versions of your files, the original and compressed versions. This means that each time you update an uncompressed file, you will need to delete and recreate the compressed copy of it.
  2. Compressed HTML files don't get put through the Server-Side Includes processor.
  3. With the language method, if any of your visitors have specified in their web browsers that they can't read the language your site is written in, they will get an error page with status code 406. This page links to the right file but warns that it's in a language they don't know. Technically this is what's meant to happen, but it may look unprofessional to serve these error pages.

The only problem is actually setting Apache up to send the compressed files. While compressing the output of a PHP script on the fly gets a mention on many web designers' sites, there doesn't appear to be much documentation about doing the same thing with static HTML files, besides verbatim copies of the mod_mime related part of Apache's documentation.

So here's a step-by-step guide, just in case you have a site comprised of good, old-fashioned HTML files and want to save your visitors time and yourself some bandwidth.

The language method

  1. Edit .htaccess You need to add three lines to it. First, add the line Options +MultiViews. This enables Apache to work out what a file is, based on its extension, and give it to the user based on their preferences. Then add the line AddEncoding x-gzip .gz so that it recognizes the gzip extension and knows that it's what the user wants if their headers specify that they can accept compressed files. Lastly, add the line AddLanguage en .en (or the equivalent for whichever language the files are written in). I'll explain why you have to do that in the next step.
  2. Rename the uncompressed HTML files If your site's visitors request file.html and that file exists, Apache will give it to them, even if they can accept compressed files and you have a compressed version of that file. For this to work, a file cannot exist with the exact extension that the visitors request. This is why you have to rename all your files so that instead of ending in .html, they end in .html.en, or your language's equivalent ISO 3166 code. Don't worry; your site's visitors will be blissfully unaware that any of this is happening.
  3. Make compressed copies of the HTML files Now you can make the compressed copies of your files. Unfortunately, the gzip program doesn't appear to have the ability to keep the original file as well as the new, compressed version, so you'll have to create the new files one at a time like this: gzip -c --best file.html.en > file.html.en.gz. Remember that if you later edit one of the original files, you'll need to delete the compressed version of it then create a compressed version of the new file.
  4. Test your site There are several ways you can test whether this all worked, most of which involve complicated things like using telnet or viewing the HTTP response headers in your browser. If you can do the latter, look for a line which reads Content-Encoding: gzip. Don't worry if you don't have such a fancy browser, though, as there's a much simpler way of testing which version of the files you are looking at: make a file called test.html.en with a line in it along the lines of This is a compressed page, then compress it. Finally, make a new page with the same filename that the first one had, and a line such as This is an uncompressed page. Then try to access test.html in your browser and see which message you get. When I tried this, Firefox, Amaya, Opera, ELinks, and even Internet Explorer retrieved the compressed file. Success!

The filetype method

  1. Edit .htaccess With this method, you only need to add the first two lines: Options +MultiViews and AddEncoding x-gzip .gz. You don't need to specify anything about languages.
  2. Make compressed copies of the HTML files This is pretty much the same as in the other method. Just use gzip -c --best file.html > file.html.gz
  3. Change the hyperlinks to exclude the file extensions This is where this method differs from the other one: don't ever link to, say, file.html directly. Instead, link to file and let Apache work out which version of the file the people visiting your site want.
  4. Test your site Again, this is very similar to in the other method. Create test.html and test.html.gz with slightly different content, then try going to test and see which message you get.

It's worth noting that both methods don't just work with HTML files, they work with all text files. You can link to ascii.txt and have the files ascii.txt.en and ascii.txt.en.gz ready to serve, for instance, or you can link to ascii and have the files ascii.txt and ascii.txt.gz ready.

You can also offer the pages in different languages, but that's for another guide.

You should hopefully now have a static web site that can send pages in both compressed and uncompressed formats (or at least know how to set one up, if you're a real geek and reading this just for fun!). Give yourself a well-earned rest.

To speed things up, here's an .htaccess file to copy and paste:

Options +MultiViews
AddEncoding x-gzip .gz
AddLanguage en .en
In addition to the above, you may also wish to try The not-ass-backwards method
  1. Install mod_gzip.
  2. Configure mod_gzip.
  3. Enjoy your intellectual superiority.
You see, mod_gzip gzips and serves compressed versions of static HTML files for you, so you don't have to bother with compressing and serving them manually.

The install and configuration in more detail:
Download the latest version of mod_zip. This example uses version 1.3.26. Adjust accordingly.

tar -zxvf mod_gzip-1.3.26.1a.tgz
cd mod_gzip-1.3.26.1a

make APXS=/usr/local/apache/bin/apxs
make install APXS=/usr/local/apache/bin/apxs
Then edit your httpd.conf file
vi /etc/httpd/conf/httpd.conf
and add the following (in the AddModule stuff):
first uncomment:
#LoadModule gzip_module libexec/mod_gzip.so
to
LoadModule gzip_module libexec/mod_gzip.so
and
#AddModule mod_gzip.c
to
AddModule mod_gzip.c
and after AddModule mod_gzip.c add this below
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_can_negotiate Yes
mod_gzip_static_suffix .gz
AddEncoding gzip .gz
mod_gzip_update_static No
mod_gzip_command_version '/mod_gzip_status'
mod_gzip_temp_dir /tmp
mod_gzip_keep_workfiles No
mod_gzip_minimum_file_size 500
mod_gzip_maximum_file_size 500000
mod_gzip_maximum_inmem_size 60000
mod_gzip_min_http 1000
mod_gzip_handle_methods GET POST
mod_gzip_item_exclude reqheader "User-agent: Mozilla/4.0"
mod_gzip_item_include file .html$
mod_gzip_item_include file .shtml$
mod_gzip_item_include file .htm$
mod_gzip_item_include file .shtm$
mod_gzip_item_include file .php$
mod_gzip_item_include file .phtml$
mod_gzip_item_exclude file .js$
mod_gzip_item_exclude file .css$
mod_gzip_item_exclude file .wml$
mod_gzip_item_include file .pl$
mod_gzip_item_include file .cgi$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/html$
mod_gzip_item_include mime ^text/plain$
mod_gzip_item_include mime ^httpd/unix-directory$
mod_gzip_item_include mime ^application/x-httpd-php
mod_gzip_item_include handler ^perl-script$
mod_gzip_item_exclude mime ^image/
mod_gzip_dechunk Yes
mod_gzip_add_header_count Yes
mod_gzip_send_vary Yes

Then restart apache with:
service httpd restart
That's it! You should now have a working install of mod_gzip.
Great idea for people who don't have root access! One refinement: you can simplify making the compressed copies by doing
zip temp *html.en
gzip --best *html.en
unzip temp
That way you don't have to do it file by file. A further refinement, I suppose, is that if you edit some of the files and want to update the gzipped ones you should first
rm *gz
and then run the commands given above.

Log in or registerto write something here or to contact authors.