Simply Enable PHP’s GZip Compression to Speed Up Your Blog

As you may know, I use WP SuperCache on all of my blogs to enable them to handle the rather high loads the server runs at occasionally. One of the things that WP SuperCache does is enable compression for all of the cached pages. It’s an option in the control panel, which I ALWAYS enable. This saves a lot of bandwidth and makes pages download much faster for readers.

Well, what I didn’t realize was that the compression is only being performed on the cached files apparently. Because I did some testing and on the non-cached files there was no compression. So, I’ve got a really, really easy trick that anyone can do on their PHP blog to enable GZip compression.

Of course I’m going to show you how to do it in WordPress because that is what I know – but a simple one line addition to your theme file will enable it on any PHP based system (Drupal, Joomla, or whatever). If you have instructions for another CMS please let me know and I’ll add them here too.

What we are going to do is add a very simple line that tells your server to compress all of the HTML pages that it creates using the build in function that PHP has. In order to do this in WordPress all you need to do is go into your Admin control panel, and choose the Theme Editor under the Appearance menu.

Instructions

  1. Select the Header.php template
  2. Add the following line to the very beginning of the template (before anything else)
    <? ob_start("ob_gzhandler"); ?>
  3. Save the template
  4. Have a cold beer.

That’s it. Your job is done.

GZip Compression Improvement

What will this do for you? Well, in my case lets look at the before and after results from OneMansBlog.com. These results were measured by the Firebug plugin for Firefox and each example was the best of three back to back pageloads. I also enabled the compression immediately between the before and after reloads to try and ensure similar load circumstances. The test was conducted via a 15mbps cable internet connection.

Prior to implementing GZip Compression the homepage was 99.6kb and took 1.96 seconds to download (click the image to view full size):

After turning on GZip compression in WordPress the homepage was compressed to 18.3kb, and it took only 1.15 seconds to download. The 80% page compression saved about 40% of the download time (click the image to view full size):

Once you’ve enabled compression you can go here to test your Webpage to see if it is working.

Whoopee! 1 Second. Who Cares?

Hold on there honcho. I managed to save about one second on just one pageload. On average I routinely get over 20,000 page loads per day. So that would be 20,000 seconds – or 5.5 hours – per day saved. That is 2,027 hours per YEAR – or the equivalent of an entire full time employee.

In addition, we saved over 80kb of data from being downloaded or 1.6 gigabytes per day. That is almost 600GB per year (128 complete DVDs) in what would have been completely wasted bandwidth.

Now, just imagine if everyone did this how much time we would save, and how much faster the Internet would be going. Heck, its a moral imperative for everyone to turn on compression!

So, what are you waiting for? Your readers are begging you to speed things up for God’s sake!

PS – You can try and find a plugin to do this for you too. But it’s so easy why not just add the line and be done with it?

Article Written by
John P.

John P. is CEO of Livid Lobster and co-host of Geek Beat TV. You can also find him on Twitter, Facebook and Google+.

Comments

  1. Just wish to say your article is as surprising. The clearness on your post is simply excellent and i can assume you’re knowledgeable in this subject. Fine together with your permission let me to seize your feed to keep updated with forthcoming post. Thanks one million and please continue the rewarding work.

  2. Thanks so much for this terrific tip on adding the code in the header.php to speed up site load pages! It worked like a charm and my pages are loading much, much faster. Still have some other things to work on though, like image lossing and some other stuff, but at least things are looking up, and this tip was soooo easy!

  3. Seo Uzmanı says:

    thanx is hosting problems :S

  4. Adam D says:

    Wow…
    Original Size: 76.21 KB
    Compressed Size: 11.53 KB
    Data Savings: 84.87%

    That’s not bad at all!

  5. ansh says:

    can i use this with wp-total cache?

  6. igecorner says:

    Great Tips, it works

    Thanks

  7. Lionel says:

    Thanks for this informative post. My website is being gziped after following your tips.

  8. Govpatel says:

    I tried and the script @Eddy suggested and got 500 error I am using Ultimate Seo any one have same problem.
    I added as author suggested and got a blank page then I added php so it is

    even then I got blank page.
    Any suggestions

  9. adi says:

    adimahardika.com
    Original Size: 95.41 KB
    Gzipped Size: 15.64 KB
    Data Savings: 83.61%

  10. adi says:

    I thank to you John. This is a great solution.

  11. @iTechRoom – Yes, it conflicts with All in One SEO. It breaks the rewriting of the title tag.

  12. iTechRoom says:

    Does this code conflicts with WP Super Cache plugin or WP All in one SEO plugin, It’s showing “Content Encoding Error” during first visit but works on refresh.

  13. The very use plug HiperCashe, but the simplicity of the method was intrigued.

  14. iTechRoom says:

    It’s a very simple but very useful tips. Checking the results for my site. Thanks for sharing

  15. Tech Junoon says:

    Load time before WP Cache enabled – 0.69 seconds
    Load time after WP Cache enabled – 0.36 seconds
    Amazing…!

  16. Tech Junoon says:

    I have just checked my site. See the results below. It is a huge difference, really.

    Actual Page Size: 39.39 KB
    Size if Gzipped: 9.19 KB
    Potential Savings: 76.67%

  17. Matt says:

    Not working for Drupal.
    Getting the Content Error:
    The page you are trying to view cannot be shown because it uses an invalid “or unsupported form of compression.”

  18. Nathan says:

    Quick update…this greatly decreased my load time, but it is not really compatible with the All in One SEO plug in – it breaks the title tag. FAS, this could also be the reason for the broken home page title. Are you using All in One SEO plugin?

    For now I’ve removed gzip, installed WP Super Cache, and will consider using another SEO plugin so that I can use gzip.

    Thanks for the great tip…hope to use it again soon!

Speak Your Mind

*