How to gzip static pages

Gzipping static pages reduces the total amount of bandwidth you use and reduces page load times by serving smaller files.

Make your site serve gzipped versions if available

To make your site serve the gzipped version if it is available, add the following to a file called .htaccess in /home/public:
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [L]

This tells Apache to serve the file ending in .gz if it is available as if it was the original file, if the requester accepts the gzip encoding. So this assumes you have files like “index.html” and “index.html.gz” or “program.js” as well as “program.js.gz”.

Gzip your files

From an ssh shell command line, do “gzip –keep file.html”, where “file.html” is the name of the file you want to compress. The –keep option stops gzip from deleting the original file. If you have a lot of files, here is a Perl script which automates the gzipping process:
#This script should be uploaded to the web server.
use warnings;
use strict;
use File::Find;
find (\&wanted, ("."));
sub wanted {
if (/(.*.(?:html|htm|css|js)$)/i) {
print "Compressing $File::Find::name\n”;
system ("gzip --keep --best --force $_");

You can download the script from my Github repository. You should run this from a ssh shell command line in the directory /home/public. Run


It will find and compress any file ending in .html, .css, or .js (case insensitive).

Test whether it’s working

To test whether the gzipping is working OK or not, there is Type your URL into the box and it will give you a green tick if compressed content is being sent OK. Google webmaster tools also has a facility to do this, but it’s not quite as instant as

Removing the uncompressed files

If you want to remove the uncompressed files and keep only the compressed ones, this project on github contains a complete solution in Perl which also uncompresses compressed files for browsers which don’t accept the gzip encoding. This reduces the storage charge.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s