How to GZIP Your Site’s Content
by jim on December 30th, 2009GZipping your content is the easiest and best way to speed up your site. For a full discussion about Gzipping content, I recommend this page on gzipping content (the code snippets I present below are taken from that site). If you just want the nitty gritty, without the technical explanations, as well as some real world results, let’s jump right into it.
The easiest way to gzip your content, if your site’s server supports it, is to throw one of these lines in your .htaccess file:
# compress all text & html:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
# Or, compress by file extension:
<Files *.html>
SetOutputFilter DEFLATE
</Files>
Be sure to change the .html to .htm if you use that as your HTML file extension.
Or, if you don’t have access to your site’s .htaccess file, throw this PHP snippet at the top of every page (in WordPress, throw it in your header.php file):
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>
I was helping Jeff Rose of Good Financial Cents navigate the various “tips” in YSlow and the first one we knocked out was GZipping all of his content. With that one change, we trimmed his homepage size from 805.5K to 706K (unprimed cache). A 12.4% reduction in page size with the addition of one line… not bad!
After you’ve made the change, check to see that it worked using this site’s gzip test.
Like this article? Get all the latest articles sent to your email for free every day. Just click "Subscribe" and enter your email. Your email will only be used for this daily subscription and you can unsubscribe anytime.

December 31st, 2009 at 12:19 am
Thanks for this great tip, Jim! I was amazed at how much it sped up my site – especially one page that had a TON of text.
December 31st, 2009 at 12:52 am
Hmph, well it sped up my site, but now WP Super Cache isn’t caching pages anymore. I had to use the PHP code because the .htaccess stuff messed up my site. (I think it was a 500 error or something.)
I’m hosting on GoDaddy – could that be my problem?
Is it more important to have gzip working than WP Super Cache?
December 31st, 2009 at 1:06 am
Well, I think it’s working now. I had to add this after the in my footer:
I don’t know why, but at least WP Super Cache seems to be working and my site still passes the gzip test.
December 31st, 2009 at 9:13 am
Paul, your code didn’t come through, mind emailing it to me and I can add it manually to the comment?
December 31st, 2009 at 9:14 am
Cache is much more important… I suspect the caching is still occurring, just not displaying the note.
December 31st, 2009 at 9:14 am
And it’s a lossless change, nothing on your site changes and you only gain in terms of speed.
December 31st, 2009 at 10:22 am
Another great tip, Jim. Implemented this in a snap through htaccess. One site’s home page went from 31.8K to 7.4K. Another site went from 46 to 16.
I assume this code does not compress javascript. Is that right, and if so, do you have a magic code snippet that compresses js?
December 31st, 2009 at 10:27 am
Well, the code I put in the comment doesn’t actually help.
It was strange. It started working for a bit, but then it stopped. (Not sure why…I didn’t change anything after it was working.) So I gave up last night.
Now I went back through, deleted the code I put in and the code you have above, turned WordPress Super Cache to Half-On mode, and now it seems to work. I had to use half-on because GoDaddy doesn’t have the mod rules installed on the shared server I’m on.
Only thing that seems strange now is when I use the gzip test you linked to it works, but when I use ismyblogworking.com it says it’s not compressed. It does seem to be compressed to me though – my huge page of text loads a lot faster than the old days.
January 1st, 2010 at 11:39 am
[...] your site’s load times, you can lower the amount of data that gets sent (that’s what gzipping your site accomplishes), or you can lower the number of HTTP requests. Every HTTP request takes a small [...]
January 2nd, 2010 at 3:31 am
Where in the .htaccess file does this go? I tried a couple of places but I get a an error when I reload.
If I used the PHP where in the header would I put it (I’m using the Thesis theme)?
Thanks!
January 2nd, 2010 at 10:47 am
It should work anywhere in .htaccess, that’s processed first.
When I worked with Jeff, I had to put it in the header.php file. I tried to use the hooks but I couldn’t get it to work properly because there isn’t a hook at the very beginning of the header file (earliest one is in the title or before the body, if I remember correctly).
January 3rd, 2010 at 6:11 pm
I just did this on media temple through my server settings file.
After doing a test it looks like this reduced mine by like 75%
Original Size: 51.44 KB
Gzipped Size: 12.8 KB
Data Savings: 75.12%
Thanks Jim!
(although now that i check it through yslow, it’s not showing it gzipped?)
January 3rd, 2010 at 8:12 pm
Wow, that’s some serious savings there… according to the GIDZipTest, it’s gzipped so I wouldn’t worry about it.
January 3rd, 2010 at 8:29 pm
Awesome… once again thanks for the advice.
My site size decreased by 25%.
January 3rd, 2010 at 11:00 pm
I take that back, it decreased by 75%. Here are the numbers:
Original Size: 42.23 KB
Gzipped Size: 10.98 KB
Data Savings: 74%
January 18th, 2010 at 2:01 am
thanks for the tip about implementing it inside of wordpress. My htaccess file gave me a 500 error when I added it but the change to the header.php worked like a charm. 69% compression, thanks!