0

I have this minimal code that outputs some text:

<?php

$output = "";

for ($i = 0; $i < 7000; $i++) {
    $output .= ($i % 2) ? "Foo " : "Bar ";
}

header("Content-Length: ".strlen($output));
echo $output;

exit;

Using Apache 2.2.34 on my web server, and I'm unable to compress the output via .htaccess (see below).

But if I simply remove this header("Content-Length… line, suddenly the output is compressed as expected and the appropriate headers are sent (Content-Encoding: gzip, Vary: Accept-Encoding, Transfer-Encoding: chunked).

My .htaccess uses the AddOutputFilterByType and Filter* directives, I’ve tried both independently as well:

AddOutputFilterByType DEFLATE "text/html"

<IfModule filter_module>
   FilterDeclare   COMPRESS
   FilterProvider  COMPRESS  DEFLATE Content-Type $text/html
   FilterProvider  COMPRESS  DEFLATE resp=Content-Type /text/(css|javascript|plain|xml|x-component)/
   FilterProvider  COMPRESS  DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
   FilterChain     COMPRESS
   FilterProtocol  COMPRESS  change=yes;byteranges=no
</IfModule>

I'm not experiencing this issue in my local working environment, where I'm using Apache 2.4 (sadly I can't upgrade the server).

EDIT: In my case, the problem is solved, see my comments below.

sylbru
  • 1,461
  • 1
  • 11
  • 19
  • [Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, considering the Content-length header is supposed to indicate the compressed size, not the uncompressed size as is the case here. – sylbru Dec 02 '18 at 15:14
  • The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (https://github.com/PHP-DI/Slim-Bridge), and the default configuration sets the `addContentLengthHeader` to true, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding `"settings.addContentLengthHeader" => false` in my definitions array. – sylbru Dec 02 '18 at 15:15

0 Answers0