scriptygoddess

28 Mar, 2003

Compressing Webpages for Fun and Profit

Posted by: Christine In: How to's

(Written by the Guest Goddess, Photo Matt. Please note: You need to have PHP on your server to do this. No PHP? Won't work.)

So your page is now totally pimped out. You have gads of content on your sidebar, you've used ScriptyGoddess know-how to have comments and extended entries pop out like magic, and you even have some entries to take up some space between all the gadgets. The problem? The code on your page is now weighing in at half a meg and you can actually hear people cry when they load your site with a modem. You start to think about what features you could take out, maybe cutting out entries on the front page, but what if I told you that you can third your content easily with no work on your part whatsoever? It sounds like a pitch I might get in a lovely unsolicited email. The secret lies in the fact that every major browser of the past 5 years supports transparently decompressing content on the fly. There are three ways to do it—easy, right, and weird—and we'll cover all three here. Before we even get started you should check for compression of your pages, because if it's already happy it's probably best to not fix what ain't broken.

Easy

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

I hate to be anti-climatic, but that's it. Put that at the very top of your PHP-parsed page that you want to compress and that's it. The only thing to watch for is it really does have to be at the top, or the sky will fall. Actually before you call me Chicken Little, you'll probably just get a cryptic "headers already sent" error, but you can never be too careful. Basically what this magical line of code does is start an output buffer which takes all your content, checks if the client can receive compressed content, and if it can it zips up the buffer and sends it on its merry way. This can be a great technique to curb your bandwidth usage to; I've seen it save gigabytes on content-heavy sites.

Right

While the overhead associated with the above is minimal, if you'd like to see the benefits of compressed content on a larger scale, mod_gzip is the way to go. Mod_gzip is an Apache module which will compress files whether they are CGI scripts, processed by PHP, static HTML or text files, whatever it can. It is completely transparent to both the user and client, and it supports sophisticated configuration to allow it to be tweaked to your heart's content. However if you don't have permissions on your box to compile modules and modify httpd.conf, this option is unavailable, but don't let that stop you from bugging your host to include it, as there is really no good reason to not include it. It's always faster to send a smaller file. If you're interested in writing your own Apache module, studying mod_gzip is a great way to learn as it has extremely informatative debug code.

Alternative

There are certain circumstances where output buffering, which by definition has to wait for everything to process before it sends anything to the browser, can cause a perceivable delay in viewing scripts that take a while to run. With mod_gzip this isn't a problem because it streams content as it comes to it, and using PHP it doesn't have to be a problem either because it offers an alternative method of compressing and sending content, called zlib output compression. It's a little trickier to enable though, because there is no good way enable or disable it with straight PHP code, so the way we're going to do here is use .htaccess to modify the php.ini configuration. Instead of waiting until everything is finished, zlib output compression can take the content as chunks and send them as it comes to it. Here's what you need to put in your .htaccess file:

<FilesMatch "\.(php|html?)$">
php_value zlib.output_compression 4096
</FilesMatch>

Basically what this code says is if the file ends in php, htm, or html turn zlib output compression on and stream it out every 4 kilobytes. It's common to see a 2K buffer suggested on the web but I've found the overhead with that is higher, and this is a nice balance. You should know that this is the slowest of the three methods, but by slow I mean it adds .003 seconds instead of .001, so it's not really that big of a deal.

So now you have a faster site that's more fun to visit, and you're saving money on bandwidth. You can sit back now and wait for the love letters to pour in from your readers saying how much faster everything is loading. Enjoy!

Geek Notes

  • Like with so many other things, Netscape 4 really screws up gzip encoding in a lot of ways, but you can avoid 99% of its problems simply by making sure that you don't gzip any linked JS or CSS files and you should be alright. On a more technical level, early versions of Netscape 4 try to use the browser cache to store compressed content before decompressing it, which works unless you have your browser's cache turned off, and then it will do something crazy. Note that this behavior even varies from version to version of Netscape 4, so overall I wouldn't worry about it.
  • If you're doing things over SSL and you want to use mod_gzip as well, you have a little hacking to do.
  • PHP.net documentation on ob_gzhandler and zlib output compression (they recommend using zlib).
  • Things like images, zip files, and Florida ballots are already highly compressed so trying to compress them again might actually make them bigger. And then you have to recount.
  • Avoid compressing PDF files as well because sometimes Internet Explorer on Windows (the 900-pound gorilla) forgets to decompress them before the Acrobat plugin takes over.
  • According to the RFC, technically compressed content should be sent using transfer encoding rather than content encoding, since technically that's what is going on. One browser engine supports this, can you guess which one?
  • Internet Explorer on Mac doesn't support any sort of content compression like the methods described above, but that's okay because all of the above methods intelligently look for the HTTP header that signals the client can accept gzip encoding, and if it isn't there—like in IE Mac, handheld browsers, whatever—they just sit idly by.

56 Responses to "Compressing Webpages for Fun and Profit"

1 | Jennifer

March 28th, 2003 at 9:03 pm

Avatar

WOW Christine. That is impressive! Thanks! 🙂

2 | Kymberlie R. McGuire

March 28th, 2003 at 9:17 pm

Avatar

Hmmm. I thought Matt had optimized me before, but I'm not any longer. I have the php line, though, but I'm not sure if it works.

3 | adam

March 28th, 2003 at 11:22 pm

Avatar

Thanks Christine and Matt! I was forced to resort to the 3rd option, but it appears to be working. Now I'm off to bug the folks at my hosting company to install mod_gzip. 🙂

Now about those love-letters… 😉

4 | Christine

March 29th, 2003 at 1:04 am

Avatar

Oh – I can't take any credit for the post other than begging Matt to write it! HE gets all the credit for every word – I didn't edit it at all! MATT ROCKS!!!

6 | dasme

March 29th, 2003 at 1:21 am

Avatar

Thanks for posting such a useful script! I've been reading this site for a while now, and you all never cease to amaze me!

7 | Adam Lasnik

March 29th, 2003 at 2:01 am

Avatar

I'm somewhat embarrassed to write a "me too!" style entry… but let me just say that when I discovered this trick on the MT forum and implemented it on my site, there were two huge collective sighs of relief: One, from me… knowing that I no longer had to agonize of the bandwidth/feature balance so regularly… and two, from my dialup visitors, who are no doubt MUCH happier to be able to zip through my blog with at least twice as much speed 🙂

8 | cal

March 29th, 2003 at 1:06 pm

Avatar

this sounds fabulous. 'cept – i skin my site now, and have to have the cookiecheck.php file at the top. if they *both* have to be at the top, how does one juggle this?

9 | Matt

March 29th, 2003 at 1:46 pm

Avatar

Put this above the skinning code.

10 | Isuldor

March 29th, 2003 at 3:43 pm

Avatar

is it possible to do this with ASP?

11 | don

March 29th, 2003 at 3:54 pm

Avatar

Hmm, I have the mod_gzip module installed and running…. and the weigh-in pages say that my site is not using gzip…

sigh

12 | Matt

March 29th, 2003 at 4:02 pm

Avatar

Currently the web optimization link doesn't check for gzip encoding, though it really should and might in the future. The leknor.com link should be used to see if your pages are being compressed.

Don, in your case your pages should be compressed, and your server is reporting mod_gzip is installed. I would contact your host and have them check things. If you're running your own server drop me an email and I'll take a look at it; it's probably a problem with your httpd.conf file.

13 | Anabella

March 29th, 2003 at 7:51 pm

Avatar

I used the first option, and I don't see any difference whatsoever… I am using a T1 and my page loads at .36 seconds so of course I wouldn't care, but it takes 10 seconds to load with a 56k modem and that's… plain evil. Like you said, I put the php code first line of the page, and I used your link to check again and it still appears to take the same amount of time to load… Should I use another method or?

14 | Anabella

March 29th, 2003 at 8:00 pm

Avatar

BTW- I am using skins, but I did put the php line above the cookiecheck like you said…

15 | Jasmeet

March 31st, 2003 at 5:28 pm

Avatar

I used the easy method, but didn't notice a change. Probably because I'm on a high speed connection.

However, some people noticed the decrease in load time for my site, so looks like it works =D

16 | sparticus

April 1st, 2003 at 5:16 am

Avatar

Hey, I'm using the 3rd "alternative" option as I don't have access to my httpd.conf and it seems a lot easier than the easy way. It works fine for everything and it all loads faster, all but one page which includes a cgi file.. (my guestbook) now this page (http://www.iamsparticus.co.uk/guestbook/index.php) works fine in opera 7 but in MSIE (any version it seems) it fails miserabley and wont' load. I thought I should be able to fix it, by putting a different .htaccess file with modified code in the guestbook subdirectory. However I still get the same problem. the code I've stuck in the htaccess file in that directory is…

<FilesMatch "\.(html?)$">
php_value zlib.output_compression 4096
</FilesMatch>>

Any ideas on how to get it so it doesn't zip this one directory?

17 | Sekimori

April 1st, 2003 at 3:28 pm

Avatar

Actually, there is a good reason some hosts won't install the mod_gzip module…if it's available, people will use it, and use it a lot, resulting in heaver loads on servers. In a shared hosting environment, that's A Bad Thing. Never hurts to ask though, just know this is why we say no.

18 | Adam Lasnik

April 1st, 2003 at 3:33 pm

Avatar

Sekimori… but wouldn't that slight increase in server usage be rather offset by the dramatic DECREASE in bandwidth? Using gzip on my site (http://blog.smilezone.com/) has cut my bandwidth usage literally in half.

19 | Matt

April 1st, 2003 at 4:23 pm

Avatar

Sekimori, in addition to the bandwidth conserved that Adam mentioned you'll find that especially when communicating to clients over thin pipes mod_gzip will actually reduce server load because that'll be anywhere from 50-85% less time the server's TCP/IP subsystem has to remain open for the request. Plus if you're still concerned about it increasing load you can tweak the gzip settings to be lower than the default of 6, look for the line

gz1->level = 6

and play with it to your heart's content. My personal tests with Apachebench plus my research on the matter have made it a no-brainer to enable mod_gzip on the servers I administer.

20 | Mandy

September 26th, 2003 at 10:45 pm

Avatar

Hi! I am trying desperately how to turn off mod_gzip in my .htacess file. My webhost refuses to turn it off for me on his end and one of my syndication sites is receiving a parsing error because of this.

I know this article is about enabling but can anyone disable? Thanks!

Mandy

21 | Greg

March 16th, 2004 at 7:51 am

Avatar

the scripts is great but it has a problem with my cgi include is there a way to fix this i need that include but i also need to compress the page its way too big

22 | David Gillies

March 16th, 2004 at 5:05 pm

Avatar

A quick way to test whether your mod_gzip is working is via a telnet session:

$> telnet <your host> 80
GET / HTTP/1.1
Host: <your host>
Accept-Encoding: gzip
Connection: close

where you replace <your host> with your server's name. If you see a load of gibberish returned, like this:

HTTP/1.1 200 OK
Date: Tue, 16 Mar 2004 21:57:13 GMT
Server: Apache/1.3.29 (Unix) mod_gzip/1.3.26.1a PHP/4.3.4 mod_ssl/2.8.16 OpenSSL/0.9.7c
X-Powered-By: PHP/4.3.4
Connection: close
Content-Type: text/html
Content-Encoding: gzip
Content-Length: 1024

­W]s£6}¿B«vv@;Íî?ªxÂ$*oÍx­¥i;'F:þ]°wQm2ý§PÊø4õ°TO) ¥
Ö^°¾lúÄ=ÖGÐÞ¹^³³¬ZÐL½+5æ?¼ªÂÊx®5CO?¼ìW>ùh{æã¿ÜÛ ¶nQ­P²{Xèý:l#)Â
gð~ý&ÍÕuÉÚôÜÙWõëQY¾Í

then you know your gzip encoding is working

23 | Greg

March 16th, 2004 at 5:35 pm

Avatar

ohh i know that gzip is working i used the port80 soft website to check but a cgi file included in one of my pages causes a page to show blank when i use this http compression method 🙁

24 | Big Pink Cookie

March 28th, 2003 at 8:59 pm

Avatar

Make Your Site Go ZOOOOM!
Remember when I said that PhotoMatt implemented voodoo code on my site to make it faster? Did you notice how…

25 | Lockergnome's Bits and Bytes

March 28th, 2003 at 9:29 pm

Avatar

Compressed Pages = Faster Loading Times
"So your page is now totally pimped out. You have gads of content on your sidebar, you?ve used ScriptyGoddess know-how to have comments and extended entries pop out like magic, and you even have some entries to take up some space between all the gadget…

26 | Lockergnome's Bits and Bytes

March 28th, 2003 at 9:29 pm

Avatar

Compressed Pages = Faster Loading Times
"So your page is now totally pimped out. You have gads of content on your sidebar, you?ve used ScriptyGoddess know-how to have comments and extended entries pop out like magic, and you even have some entries to take up some space between all the gadget…

27 | Ain't too proud to blog

March 28th, 2003 at 10:17 pm

Avatar

Ex-zip-it B
I'm gziped now — are you? (Link via Christine and Scripty Goddess.)

28 | gessaman.com

March 28th, 2003 at 11:41 pm

Avatar

Locked, Loaded and Gzip'd
Thanks to Photo Matt and Christine my site should be loading between 3 and 20 times times faster (or so says the mod_gzip docs). All it took was a little…

29 | TooMuchSexy.blog

March 28th, 2003 at 11:48 pm

Avatar

Gziped Website
ScriptyGoddess has provided me with the know-how to compress TooMuchSexy and make everyone's webbrowsing that much faster. All it requires you to do is add one line of code and have PHP. Unfortunatley it does not work in MacIE or Safari. The code is as…

30 | TooMuchSexy.blog

March 28th, 2003 at 11:50 pm

Avatar

Gziped Website
ScriptyGoddess has provided me with the know-how to compress TooMuchSexy and make everyone's web-browsing that much faster. All it requires you to do is add one line of code and have PHP. Unfortunatley it does not work in MacIE or Safari. The code is a…

31 | dasme.org

March 29th, 2003 at 12:05 am

Avatar

Compressing Webpages for Fun and Profit
Compress that site! Watch yourself!

32 | Neil's World

March 29th, 2003 at 3:26 am

Avatar

Zipped up like a kipper
One of the new 'features' of my host account is a monthly transfer limit of 2GB – f2s had no formal limit but if you sucked up excessive bandwidth you would be charged extra. Up until the old host account…

33 | Couchblog

March 29th, 2003 at 5:56 am

Avatar

gzip on the fly
Also irgendwie fühle ich mich ein wenig ertappt: So your page is now totally pimped out. You have gads of

34 | KevinDonahue.com

March 29th, 2003 at 10:13 am

Avatar

Room-a-zoom-zoom!
Totally amped to get this loaded into our websites and ((hopefully)) speed up our weblogs for those of you with

35 | KevinDonahue.com

March 29th, 2003 at 10:19 am

Avatar

Room-a-zoom-zoom!
Totally amped to get this loaded into our websites and ((hopefully)) speed up our weblogs for those of you with

36 | KevinDonahue.com

March 29th, 2003 at 10:21 am

Avatar

Room-a-zoom-zoom!
Totally amped to get this loaded into our websites and ((hopefully)) speed up our weblogs for those of you with

37 | i am sparticus v.9.0 lyrics get you hits off google

March 29th, 2003 at 6:02 pm

Avatar

Go Speed Site Go!
For all those who, whenever they load this lovely site clock how long it takes, you'll have noticed a sudden

38 | J : Da Blog

March 29th, 2003 at 11:58 pm

Avatar

Speeding ticket!
Does the site load any faster for you guys? I used that little hack over at ScriptyGoddess…

39 | photojunkie

March 30th, 2003 at 3:41 am

Avatar

You too can optimize
Remember how earlier this week I was praising Matt for optimizing my site. He is finally ready to share his secrets. You too can speed up your site, with one

40 | Doozy

March 30th, 2003 at 8:46 am

Avatar

Compressed Pages
I've enabled compression on my .php and .html pages so that hopefully they will load a little faster and

41 | TechnoBlog

March 31st, 2003 at 7:10 am

Avatar

GIVE ME SPEED!
I've put some code on my page to speed it up. Let me know if it seems any faster. Want…

42 | Big Pink Cookie

March 31st, 2003 at 12:06 pm

Avatar

GZip, Not Just for Websites Anymore…
Just last week I mentioned using GZip to compress my website. Now it seems that scientist are asking File Compression:…

43 | Bozzy's World

March 31st, 2003 at 9:24 pm

Avatar

note to hosted blogs
I added a little snip php code to the beginning of all your index pages, in some cases I had…

44 | identity

April 2nd, 2003 at 4:18 pm

Avatar

Compressing Webpages
Photo Matt has written an article called Compressing Webpages for Fun and Profit (Posted by Christine)….

45 | anything but ordinary

April 7th, 2003 at 11:58 am

Avatar

Under Construction
Lisa just reminded me that I neglected to tell you all about the site changes I made this weekend. I'm actually still working on some things, but I wanted to let you know what I've done so far. The Zonkboard…

46 | Thoughtprints

April 7th, 2003 at 8:09 pm

Avatar

In the Quest for Speed
Following M.'s (anything but ordinary) lead, I have gzipped my front page in the hopes of making it load faster. If you notice a difference in load-time, could you let…

47 | in nancy's book

April 8th, 2003 at 2:12 pm

Avatar

I feel the need for speed
I changed two things. So hopefully you should see some improvements on how fast the front pages loads. I used…

48 | beautiful-friend.net

April 8th, 2003 at 4:28 pm

Avatar

gzipped
I gzipped my front page. It should make things load more quickly. Can anyone say that it has?…

49 | My friggin' blog!

July 3rd, 2003 at 9:34 am

Avatar

mod_gzip
scriptygoddess What a helpful read, thanks! Just got mod_gzip setup on my site, and it cut the page sizes by about 70% according to the logs. WOW! I really wish …

50 | Silent Running

July 12th, 2003 at 10:58 am

Avatar

Serving as a lesson to others
Andrea and Tim's bandwidth scare drew my attention to the concerns of page obesity. Apparently, SR kicks out at around 200k per serving. Supersize it, baby. At this rate, its only a matter of time before the sharks circling the…

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements