Forum Moderators: phranque
I've set up the following rule for .htaccess to route every .html file through the php file:
AddHandler doGZip .html
Action doGZip /gzip.php
This is the essence of the gzip.php file:
$file = $_SERVER["REQUEST_URI"];
$file_contents = file_get_contents($file);
$output = gzencode($file_contents, 9);
header('Vary: Accept-Encoding');
header("Content-Encoding: gzip");
header('Content-Length: ' . strlen($output));
echo $output;
I've stripped out tests for not supporting gzip etc.
However, when I open a page the browser shows an empty page and the HTTP header reads "Content-Length: 0".
When I comment the middle header line (the encoding) the browser shows an encoded file. When I write this file to disk the header of the file is a valid gzip-encoded signature (1F 8B 08).
When I try to immediately gzdecode the output it's empty too:
if( $f = @fopen( "encoded.txt", 'w' ) ) {
fputs( $f, $output );
fclose( $f );
}
if( $f = @fopen( "decoded.txt", 'w' ) ) {
$output = gzdecode($output);
fputs( $f, $output );
fclose( $f );
}
What am I missing? Thanks!
When I apply the encoded content to this function it successfully decodes the webpage.
But my browser doesn't.
Any clues warmly welcomed. Thank you.
I'm using the .htaccess rule to redirect to the php file which does the rest.
As I understand it the gzencode command creates the entire file including the header. I have checked the spec (http://www.gzip.org/zlib/rfc-gzip.html) and compared the first few header bytes which are correct.
The file the browser decodes shows in Firebug "Content-Type text/html", so I assume that's encoded correctly. However, it does also show "Content-Length 0". Closer inspection of the spec reveals that the uncompressed file length should go into the last byte (CRC32 and ISIZE), but that byte is #00 in my tests.