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

February 22, 2010

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?

Related posts

  1. WordPress: Homepage Ads ONLY After The First ArticleShareHave you ever wondered how to show Google ads on your WordPress blog’s home page, but only after the very...
  2. Tutorial: Password Protect an Entire WordPress BlogShareRecently I had the need to set up a couple of blogs that I only wanted people who are logged...
  3. WP Super Cache Speeds Up WordPress BlogsShareDonncha O’Caoimh, one of WordPress’ lead developers spent the last few weeks working on a brand new plugin called WP...
  4. One Man’s Blog 1 Year Anniversary!ShareWow, has it been a year already?!? My inaugural post came on July 23, 2006. I didn’t really have a...

{ 27 comments… read them below or add one }

1 Gary Boyd February 22, 2010 at 9:05 pm

Thanks for the tip John…I have a shared hosting plan on GoDaddy so I need all the help I can get to speed things up…

Reply

2 Kevin Morris February 22, 2010 at 11:59 pm

Thanks John, I have been looking for the past couple of days on ways to trim my blogs overhead. I added the code and cut my download by 80%.

Reply

3 Mattias February 23, 2010 at 3:21 am

Hi John and thanks! That saved 67% on my site. Appreciate the tip!

Reply

4 patung February 24, 2010 at 1:23 am

“Content Encoding Error

The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.”

That’s if you use google cse search on your site with the default header.

Reply

5 fas February 24, 2010 at 8:57 am

Thanks a ton, just added it. Is working fast, cant thank you enough :)

Reply

6 Mark Konen February 24, 2010 at 12:17 pm

I put that code in but Firefox said it was unsupported file compression and would not render the page. Did not work in Safari either (mac versions both). Does WP SuperCache have to be enable for this code to work?

Reply

7 Hans Schmidt February 24, 2010 at 10:00 pm

Great find. But I still would prefer a plug-in ;-) Maybe WP should just come with Supercache by default, that would surely help and we could donate some bytes to people with less bandwith.

Reply

8 fas February 26, 2010 at 9:27 am

Ok John, its woriking but archives and sitemap are not showing up. It says The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression. This happens when the code you have given is added. Any solution?

Reply

9 Pacman February 26, 2010 at 10:01 am

I never thought of that. Thanks! My friend use to tell me that Gzipping is bad for your site. Maybe it was because he doesn’t know how to do it.

Reply

10 Chris Roane February 26, 2010 at 10:22 am

Great article. I implemented this tip into my wordpress blog and I can already see a difference.

I also would recommend WP Super Cache as a plugin that you need to have for your wordpress.org blog. It is invaluable!

Reply

11 John P. February 27, 2010 at 1:21 am

That is weird. I can’t understand why it would work on some pages, but not others. And is your sitemap auto-generated? It shouldn’t even be using the same header.php file as the other stuff!

John

Reply

12 John P. February 27, 2010 at 1:21 am

In the past some browsers could not handle GZiped pages. But like 99% do now so it’s not a big deal.

Reply

13 fas February 27, 2010 at 1:34 am

No sitemaps are not auto generated, sitemap and archives templates are there in the theme. Any solution to the problem? How to exclude sitemap and archives in the GZip compression.

Plus sometimes the home title changes to something else with this active.

Reply

14 Konstantin Boyandin February 27, 2010 at 6:45 pm

The compression mentioned may do wonders, especially if quasi-static pages are used (as those generated by Super Cache).

I would only add that using Apache’ mod_deflate and a .htaccess directive

SetOutputFilter DEFLATE

(remember, you can apply it to any type of file using Files directive)

will apply the GZip compression to files not served by the WP or whatever engine do you use. If you allow downloading files that do compress even a while, that will donate to efficiency.

Reply

15 fas March 2, 2010 at 3:05 am

To those whose sitemaps and archives are not opening, add this code

Where X and Y are your pages to exclude from gzip compression.

Reply

16 fas March 30, 2010 at 8:08 am

JOhn, Can you tell me why does the gzip cause the name of the home page title to change?

Reply

17 JP March 30, 2010 at 10:25 am

If anyone suffers from ‘Content Encoding Errors’ after enabling GZIP, double check there is no whitespace before gzip is loaded in the header. I had this and it stumped me for weeks!

Reply

18 Brad April 24, 2010 at 5:05 pm

Thanks for the tip and great explanation. I just finished reading quite a few tips on compression and wish I would have found yours first :)

Reply

19 Eddy June 9, 2010 at 12:41 pm

An alternative solution but guaranteed to work.
If you have an access to .htaccess file in your Wordpress root folder, add this script at the very bottom: (just copy and paste)

SetOutputFilter DEFLATE

# Netscape 4.x has some problems
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48, the above regex won’t work. You can use the following
# workaround (comment the above line and uncomment the below line) to get the desired effect:
# BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don’t compress already-compressed files
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

# AddDefaultCharset UTF-8
AddType text/x-component .htc

php_value upload_max_filesize 128M
php_value post_max_size 128M
php_value memory_limit 256M

php_flag zlib.output_compression On

Reply

20 Nathan June 25, 2010 at 8:36 pm

Thanks for making this so easy. I’m always intimidated by things like this, and the previous suggestions I had read about how to implement gzip compression were beyond me. I added your code to my site in just a few seconds…
Now I have more time to enjoy that beer!

Reply

21 Nathan June 28, 2010 at 10:52 pm

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!

Reply

22 Matt August 8, 2010 at 4:58 pm

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.”

Reply

23 Tech Junoon August 10, 2010 at 2:03 pm

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%

Reply

24 Tech Junoon August 10, 2010 at 2:15 pm

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

Reply

25 iTechRoom August 30, 2010 at 2:33 am

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

Reply

26 Blogger Xstroy August 31, 2010 at 6:21 am

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

Reply

27 iTechRoom September 2, 2010 at 12:03 pm

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.

Reply

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post:



Copyright Livid Lobster 2010