How to GZIP Your Site’s Content

by jim on December 30th, 2009

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

RSS Subscribe 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.

16 Responses to “How to GZIP Your Site’s Content”

  1. Paul Williams Says:

    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.

  2. Paul Williams Says:

    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?

  3. Paul Williams Says:

    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.

  4. jim Says:

    Paul, your code didn’t come through, mind emailing it to me and I can add it manually to the comment?

  5. jim Says:

    Cache is much more important… I suspect the caching is still occurring, just not displaying the note.

  6. jim Says:

    And it’s a lossless change, nothing on your site changes and you only gain in terms of speed. :)

  7. DR Says:

    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?

  8. Paul Williams Says:

    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.

  9. » Combining Multiple CSS Files Says:

    [...] 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 [...]

  10. FFB Says:

    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!

  11. jim Says:

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

  12. Peter Says:

    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?)

  13. jim Says:

    Wow, that’s some serious savings there… according to the GIDZipTest, it’s gzipped so I wouldn’t worry about it.

  14. Matt Jabs Says:

    Awesome… once again thanks for the advice.

    My site size decreased by 25%.

  15. Matt Jabs Says:

    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%

  16. Ben Says:

    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!

Leave a Reply