Speed up your web pages

14 March 2008 - 11:21

Surveys about web usage habits show that many web users are impatient, and will go somewhere else if a web page doesn't load within a couple of seconds. There are many factors that can affect the speed of a web page - the size of the page itself, the speed of the web server, and the speed of the connection between it and the user. It may not be easy to do much about these things. But there is an easy way to get a 4 times increase in the speed of your web pages.

A month ago I wrote about how I switched the web server on which tech-pro.net runs so that all the pages used the PHP processor, even if they had a .html or .shtml extension. I did this so I could start to take advantage of PHP scripting on the pages, without changing the page URLs and possibly affecting their Google page rank.

Another benefit of switching to PHP is to use the PHP processor's ability to compress pages on the fly before sending them to the user. It's a little-known fact that web browsers - or at least, 99.9% of the web browsers currently in use - are able to read compressed web pages. When requesting a page from a web server, the browser indicates that it supports compression. The web server will then respond by sending a compressed version of the page, if it can, which the browser will decompress before interpreting it. The format of compression used is "gzip", and it can reduce a web page to around a quarter of its size, meaning that it takes a quarter of the time to send to the browser.

If you are using an Apache web server and have only static .html pages, one way to take advantage of this - so I'm told, although I haven't tried it - is to zip all of your pages using the gzip utility, so that each page has a corresponding .gz version alongside it. In other words, you would have both index.html and index.html.gz on the server. If the web browser says that it supports compression, Apache will send the filename.gz version of the file. However, there is a danger of forgetting to update the compressed copies each time you change your web pages, and there is no easy way to generate the compressed copies automatically. Getting the server to generate zipped pages for you is a much more reliable method.

If you are using PHP to generate your pages then you can getv them compressed on the fly using the command ob_start("ob_gzhandler"); before anything is output to the browser. If your pages were originally static html pages then you can simply insert:

<?php ob_start("ob_gzhandler");?>

at the start of every file. The results will be immediately obvious even to those visiting your site using a broadband internet connection. If they are using a dial-up modem the speed increase will be even more noticeable. As a side benefit, you'll reduce the data traffic from your web server. To see just how much difference gzip compressing your web pages makes, use the What'sMyIP.org mod_gzip test. This shows that the savings on tech-pro.net pages are around 75 per cent.

Gzipping your web pages won't make any difference to the size or speed of images and other files downloaded by your web pages. Images are probably in GIF, PNG or JPG format and already compressed, so there isn't much you can do to improve on that. You won't see a 75% reduction in your server bandwidth usage unless your web pages consist entirely of text.

Style sheet .css files used by your web pages won't be compressed either. Style sheets are usually required for every page, but as they are cached by the web browser they are only downloaded once for the first page the visitor views, so there is less to be gained by compressing them. It is possible to compress them by turning them into PHP files and having the PHP processor generate them, but it turns out that this can cause them not to be cached, so the browser downloads a fresh copy for every page, which can increase data transfer if a visitor views several pages. Overcoming this is a bit more complex, and will be the subject of a future posting.

Used tags: , , , , , ,

« Internet kills the ra… | Home | A walk on the wild si… »


no comments

Trackback link:

Please enable javascript to generate a trackback url


Leave a comment
  
Remember personal info?

Emoticons / Textile
  (Register your username / Log in)

Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.