Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
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 '12 at 15:44

1 Answer 1

up vote 4 down vote accepted

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
</Location>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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