I'm having some issues running zlib compression on my site ..

Firstly if I enable zlib compression in .htaccess using: php_value output_handler ob_gzhandler then I receive thousands of lines of the following error:

PHP Warning: Cannot modify header information - headers already sent in Unknown on line 0

I have checked all files used for whitespace and any other characters that shouldn't be there with a hex editor but I could not get that error to subside with zlib turned on in .htaccess.

Therefore I have tried to turn zlib compression on in the php.ini file using:

zlib.output_compression "1"
zlib.output_compression_level "1"

However I'm now told by compression checkers that I have no compression turned on so it doesn't appear to be in the correct format?

  • The ob_gzhandler approach requires you to buffer whole output, so that modified headers (that produced by that handler) will still be able to be sent (plus, the actual compressed content). Using webserver-level compression (see @msanford answer) is much better in this case as it happens after page was finished processing by php completely.
    – LazyOne
    Apr 26, 2012 at 15:44

1 Answer 1


The first thing to keep in mind is that compression checkers sometimes lie for various reasons, so take their warnings with a grian of salt. As a few people have recently said on Stack Overflow ySlow is not gospel (but I do like it myself).

What I would suggest doing, rather than employing zlib.output_compression in php, is to enable mod_deflate in Apache. This will catch and compress static files as well (say, if you have static text files/csv/xml to serve). Keep in mind that you don't want to compress most multimedia like images and videos, since the overhead involved is proportionally inefficient when compared to the reduction in file size (which is virtually zero).

Here's an example relevant for httpd2, but there are a lot more options to choose from (check the manual):

<Location />
 # Insert filter
 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
 # Don't compress images
 SetEnvIfNoCase Request_URI \
 \.(?:gif|jpe?g|png)$ no-gzip dont-vary

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.