Hi,

I’m pretty sure this problem isnt to do with PHP, and I’ve discussed it a 
little on stackoverflow.com with another user, and come to the conclusion there 
could be a problem with Apache, but I would like an opinion.

I've written myself a custom 404 page via PHP, but I've noticed that the page 
doesn't get GZIPed when served, when I return the status code 404. Even though 
I'm sure it should be.

(I'm redirecting all requests to index.php, then parsing the URI, and issuing 
404's when needed through header(), so I'm not using ErrorDocument, although 
I've tested with ErrorDocument in .htaccess and the same problem happens.)

I'm using PHP5.5.9/Apache2.4.7 On Ubuntu (Latest for Ubuntu), and Apache is 
configured to use mod_deflate:

deflate.load: LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so
deflate.conf: AddOutputFilterByType DEFLATE text/html text/plain text/xml

This is a simple test script I've got:

<?php

header("HTTP/1.1 404 Not found");

print "WILL THIS BE GZIPED! ".str_repeat('X',500);

?>

If you comment out the header 404 (so that it gets a normal 200 response) it 
returns the page compressed fine.

I've also tried with the new http_response_code(404); command for PHP >=5.4, 
but that doesn't help.

I've tested in Chrome/Developer Tools and also directly with Curl to test if it 
gets compressed, and it doesn't. There is no Response Header 'Content-Encoding: 
gzip', so I'm guessing its Apache that's not compressing it.

Its not a major problem for me, obviously, as I hope to not get too many people 
trying to link to pages that generate a 404 error, but it would be good if it 
did compress correctly.

I can work around it, by making PHP compress it manually for my 404 page, but I 
would like to know if you think this could be an Apache problem that needs to 
be looked at and/or fixed.

(Note, you have to make sure the response is >256 bytes to get Apache to 
compress, so that's what the str_repeat() bit is)

Thanks,

Rob Donovan.

Reply via email to