Adding GZIP Compression To Your Site Via .htaccess

Here’s a tidbit of code for you, in a hope that this helps someone out. Paste the following into your .htaccess file and it will enable GZIP across all pages in the directory where the .htaccess file resides:

php_value output_handler ob_gzhandler

Hows it do it?

This line of code sets the output handler/buffer as ob_gzhandler which autosets the ob_start() function to “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:
// At the bottom:

What is GZIP?

GZIP is a very effective way of keeping your bandwidth down when having large HTML/PHP pages. What it does is it compresses the page that’s being sent to the user viewing your site. When the user recieves this compressed version of the page it uncompresses the page on their end with the browser displaying it in full to them.

But how much does this effect the user?

The user gets a faster page and it doesn’t effect them in any other way.

And what about my server?

Your CPU usage will go up slightly for a time but then it will return to normal after 30 minutes or so of users hitting your page and the server has cached gzipped pages.

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. That’s a fantastic reduction in overall download size.

Please note

GZIP does not compress images, just the raw HTML/CSS/JS code from the site being sent to the user.

49 Responses

  1. Pat

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

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

  3. Ben

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

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

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

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

    Ah, damnit.

    Ok, thanks. :)

  8. Ben

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

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

  10. Ben

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

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

  12. Ben

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

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

  14. Ben

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

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

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

  17. Alenônimo

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

    thanks, works wonders :D

  19. Av

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

  20. Tim Cinel

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

    Thanks for the info.

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

    Thanks once again

  22. Suresh P


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

    Suresh P

  23. Jangla

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

    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:


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


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

    Vielen Dank für die Informationen =)

  26. Pingback on

  27. Matthew

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


  28. trecords

    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

  29. Dave Ashworth

    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?

  30. Mi

    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!

  31. chris

    oh that rules – thank you!!

  32. Kiran

    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

  33. Jamie Huskisson

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

  34. Jesse Donat

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

  35. Jamal Panhwar

    Does this work on .html & .shtml pages aslo?

  36. Tom G

    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.

  37. Robert M.

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

  38. Pingback on

  39. chat

    does it work with any script ??

  40. francis

    I added function to some scripts the other day and got error on some pages:

    “content encoding error”
    I found out that it will generate error if page is including:

    Any workaround this issue? Unsure if this line is safe to remove..

    Other pages without the UTF-8 worked fine.

    Maybe I should use ob_start(“ob_gzhandler”); just for html ?

  41. Justin Butcher

    I’ve actually done this. It’s easy to implement and increases site speed no end. Great tip!

  42. Adam Paterson

    I had no idea you could use .htaccess enable GZIP!

  43. Suhas

    Great Tip!!

    I have added the php_value output_handler ob_gzhandler in my .htaccess file, it increases the performance but the garbage content was displaying at user front.

    Can you please let me know, if I missed something..

    Thanks in advance!

  44. Otto

    Great tip, I will add it to my htaccess. I have heard it before but don´t know how to make these changes and integrate the GZIP Compression. Awesome! Thanks! :)

  45. Shu

    Thanks for this tip!)
    Installed a gzip plug-in on one of my WP sites – it now 3 times less bandwidth. Wow )

    Why then gzip is not a standard feature? Because of the higher server CPU load?

  46. Martin

    is there any simple way to enable Gzip on HTML files too???

  47. Ashley

    Dude, this rocks.

  48. Swapnil Sarwe

    I am getting this error on my server – since some setting might have disable to avoid server load:
    Invalid command ‘php_value’, perhaps misspelled or defined by a module not included in the server configuration

    just wanted to know is there any other way I can handle the output handler

  49. Jake Ortman

    I know this is an old thread, but figured I’d list it here as it’s listed hight on Google (and I needed this code the other day).

    If your host is running SuPHP, this isn’t going to work, as SuPHP doesn’t allow php variables to be declared in .htaccess. You’ll have to have your host copy a php.ini file into your home directory, and they likely won’t do that :-)

    If you have root access on your machine, and are running cPanel, this might help:


Comments are closed, but feel free to @jhuskisson on Twitter with your thoughts.