I have trouble compressing PHP output when the Content-Length header is set. Following this very similar question of mine, it seems I've narrowed down the issue, which happens even without defining output compression in Apache's .htaccess
.
So I'm using Apache 2.4.18, .htaccess is empty, AddOutputFilterByType DEFLATE text/html text/plain text/xml
is commented out in Apache configuration, because I want the compression to happen in PHP.
My minimal PHP code:
<?php
ini_set("zlib.output_compression", "On");
$output = "";
for ($i = 0; $i < 4000; $i++) {
$output .= "Foobar ";
}
// header("Content-Length: " . strlen($output));
header("Content-Type: text/html");
echo $output;
exit;
It works fine as is, the output is compressed (it's not the case if I omit the ini_set
call, as expected).
Now if I uncomment the Content-Length
header line, suddenly the output is not compressed anymore.
EDIT: In my case, the problem is solved, see my comments below.