Adding GZIP Compression To Your Site Via .htaccess

Just thought i’d post this little tidbit of code for you. Paste it into your .htaccess file and it will enable GZIP across all pages in that directory:

php_value output_handler ob_gzhandler

Hows it do it?

This line of code sets the output handler, or output buffer as ob_gzhandler. This sets the ob_start as “ob_gzhandler” so the code doesn’t have to be entered. Normally you would have to enter this in the PHP to do GZIP Compression:

// In the head:
ob_start(“ob_gzhandler”);
// At the bottom:
ob_end_flush();

What is GZIP?

GZIP is a very> effective way of keeping your bandwidth down when having large HTML/PHP pages. What it does, is compresses the page thats being sent to the user viewing your site, when the user recieves this compressed version of the page it uncompresses the page, and views it in the full form.

But how much does this effect the user?

It doesn’t effect the user at all, except for Internet Explorer 0.01 users still on Windows 3.11 of course…

And what about my server?

Once you originally implement this, yes, you will notice changes in your processors activity. But after a day or two you’ll soon see the processor levels lower.

How much of a difference does it really make?

The answer, is roughly 80-90% of your raw code file being sent to the user is removed.

A good implementation case study for this is Pixel2Life. Before GZIP it had a 160kb file output and after a 14kb file output. This of course is brilliant for the 56kers, which alot of us forget about.

Please note

GZIP does not compress images, just the raw code being output and sent to the user.

Share this with friends:

Like what you've just read? Share it to your friends using your favourite service below:

Share on Twitter Share on Facebook Share on Stumble Upon Share on Google Share on MySpace Share on Delicious Share on Digg Share on Google Buzz Share via Reddit Share via E-Mail Share via Wordpress.com Share via Tumblr Share via Posterous Share via Newsvine

Comments:

  1. This is actually very helpful, and i implimented it on my site. And your right about the cpu spike, although things are looking to calm down now. Thanks for the info jay!

  2. Your more than welcome :) Its good to see people getting good use from tutorials

  3. I just implemented this on my website and as soon as I uploaded it, I started to get a 50o Internal server error. Hope this’ll go away soon just as a result of the cpu spike.

  4. You may not have GZIP enabled on your server if that is the case.. i’m not sure why you would get internal server errors to be honest..

  5. I made sure, and I do have GZIP enabled.
    It says in my phpinfo thing that of the loaded modules, mod_gzip is one of them. That is GZIP, right…?

  6. No this is the Apache mod_gzip that runs server side and requires no code :)

    To set this up please contact your server operator

    This is ob_gzhandler :)

  7. Ah, damnit.

    Ok, thanks. :)

  8. Is there a way to tell if I have this enabled or not without contacting my server just yet, or is the server error a good sign that I don’t? :)

  9. Ask them if output buffering is enabled, they may have disabled it to save memory

  10. I submitted a support ticket and they said that a 500 server error means I have a problem in the syntax. Annoying. I copied right off of your site. Hmm…

  11. you did only copy the first box into your htaccess yes?

  12. Yeah.
    Now this means I just have to enter ob_start(), right? Without the “ob_gzhandler.”

    I’ll play around with it when I go upstairs, where everything I have that has my website is. Right now I just used Notepad really quickly to create it and uploaded it through my host’s file uploader.

  13. No, just adding this to your .htaccess file should work fine :)

  14. I figured it out. For some reason when I pasted it on my dad’s computer (which I’m on until about 6PM when I go back up to my computer) I pasted the 1: as well for some odd reason and also didn’t notice it. God that’s stupidity for you. :)

  15. oops, in my previous post, php code didn’t get displayed, i’ll use &HtmlEntity; code to display the “less than” symbol and “greater than” symbol.

    <?php ob_start (“ob_gzhandler”);
    header(“Content-type: text/css; charset: UTF-8″);
    ?>

  16. above php code is intended for to use in the top of CSS files

  17. Your hint is almost essencial! I’ve implemented right now and I will analyse the bandwitch usage to compare…

    My entire site is PHP based. Thanks… :D

  18. thanks, works wonders :D

  19. wow, you’re a life saver :o VERY useful to cut down big files like prototype JS

  20. Heh, I’m also using it to shift heavy JS like prototype. It’s heavy but I still love it :) Oh it’s good for big CSS files, too.

    Thanks for the code :)

  21. Thanks for the info.

    Since I have access to .htaccess, can I also compress the .css files via .htaccess?

    Thanks once again

  22. Hi,

    will the above .htaccess code(php_value output_handler ob_gzhandler) also compress the Javascript / CSS files too?

    thanks,
    Suresh P

  23. When I stick this in my .htaccess file the site just outputs a bunch of garbled ascii characters – like when you try and open a compiled file in notepad or something.

  24. You can also do it in a php.ini file (assuming gzip is enabled) by adding only one line to php.ini:

    output_handler = ob_gzhandler;

    That’s it.

    Also, compressing in a PHP file, the:

    ob_end_flush();

    line is not necessary for it to function properly. PHP will flush it automatically. To simplify, all you need is:

    ob_start(‘ob_gzhandler’);

    At the top of a PHP file. You could also prepend a php file to other php files using htaccess. But if you could do that, you might as well use the htaccess example given in this post.

  25. Vielen Dank für die Informationen =)

  26. I had great luck with this! For me, to implement it server wide, I put it in the httpd.conf file on my linux server. Then, rebooted apache and BAM! Every site I host has it. :)

    Thanks!

  27. Hello,
    Very good idea, can i also compress CSS with adding this code:
    css_value output_handler ob_gzhandler

    Also i use wpmu, do i need to add the header code to all templates?
    Thank you very much

  28. This is great, though I have come across one issue with it.

    I added to a client site and everything was fine and noticeably sped up, however, the application within a sub folder displayed ascii “nonsense” – like if you were to open say a SWF file in word or notepad!

    Anyone know why this would be and how to overcome it?

  29. Thanks for the great tip! It worked on one of my ISPs. On the other ISP it didn’t. I will need to ask the service hotline why it doesn’t work there.
    Anyway, great article!

  30. oh that rules – thank you!!

  31. Hi,
    i have added php_value output_handler ob_gzhandler to compress the files in .htaccess file but am getting the following error:
    Error 500 – Internal server error

    An internal server error has occured!
    Please try again later.

    Can anybody help me wats the right way to do this.

    Thanks in Advance,
    Kiran kumar

  32. Kiran – it sounds like your server doesn’t have GZIP enabled. You’ll need to contact your host in order to do this.

  33. Once you originally implement this, yes, you will notice changes in your processors activity. But after a day or two you’ll soon see the processor levels lower.

    This is wholly incorrect. This method will compress your code, not cached in any way, every time a page is executed, output buffering the entire thing.

    Lets go over the process so we understand.

    - The output buffer begins storing output.
    - Your sites code executes.
    — Anything your code would “echo” to the user is sent to the output buffer instead
    - Your code completes
    - the output buffer containing the full content of your site is gziped
    — Gzip cannot execute on a partial document and must execute on the full completed html document
    - the final gzip result is sent to the user, once all PHP has completed

    Therefore the end user will not receive nor have displayed a single line of HTML until they receive the entire compressed document, at which point it can be uncompressed.
    This can actually create the impression that the page is loading slower because the browser will normally begin to render content as PHP flushes pieces of HTML out to the user, whereas now the user will receive it all at once, after PHP is entirely done.

    I have found in my testing on Oasisband.net that PHPs delay compressing my HTML is greater than the delay of the user downloading it uncompressed when on a relatively fast connection.

    Lastly ob_gz_handler to my understanding is more or less deprecated for Zlib Output Compression.

  34. I use it at the top of all PHP pages so that they can go out each as compressed so that the browser wont have to wait until entire thing compresses. It saves a lot of bandwidth. I don’t think this way compresses the CSS though, only the PHP/HTML.

  35. Which .htaccess would i put this in? In my wordpress theme folder or in the html root folder?

  36. does it work with any script ??

Pingbacks/Trackbacks:

  1. 14th of October 2009

    [...] Is Gzip turned on? As far as I know, yes. http://www.jhuskisson.com/articles/a…e-via-htaccess __________________ Regards, Sharky B.A. (Hons) AVATAR (JPG/PNG) | IRC | PM | MY POSTS | MY [...]

  2. 2nd of June 2010

    [...] jhuskisson.com us2.php.net Blogging, Coding, PHP, Tips & [...]

Add a Comment: