Forum Moderators: phranque
I've added this to my .htaccess to point all html requests to my gzip.php compression file:
AddHandler doGZip .html
Action doGZip /gzip.php
So every request sent to the server for any html file gets redirected to gzip.php with that file as a parameter.
So far so good.
Now I'd like to open that html file from within my gzip.php to read it, then compress and send it back to the client.
Hence the php file does a
$file_contents = file_get_contents($file);
So far so bad. This generates a html request which...you've guessed it: gets redirected by the htaccess file directive into the php file which then tries to read the file by sending a html request... ad infinitum.
I've got an infinite loop.
I have searched the web trying to find a solution to prevent the loop from happening, but all I found was to add this to htaccess:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
which I frankly don't understand 100% and has no effect either.
If a knowledgeable soul could share their wisdom with me I'd be very grateful.
Thank you!
But does this actually invoke an HTTP request, or is it a local filesystem read? For the sake of efficiency (and disregarding the looping problem momentarily) it should be the latter.
If you use a local file read, then using the following RewriteCond and RewriteRule should prevent a loop:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.html?[^\ ]*\ HTTP/
RewriteRule \.html?$ /gzip.php [L]
Jim
I used to have it just as a file request before, but this prevented me from testing if urls with parameters existed. That's why I changed the $file variable content to a url.
I used session variables to test out the loop, and got loads of session files, that's why I assume it was an HTTP request.
I agree with you that the filesystem read prevents a loop altogether, however, it leads to the problem I tried to combat, namely how to test if the file reference was
/photography/galleries/gallery1/index.html?image=7
This is an invalid file path (because of the url parameter) and this is where I got stuck.
Here's the first half of my gzip.php to further illustrate what's happening:
$file = $_SERVER["REQUEST_URI"];// Trim the string down to the format 'path/to/file.html'.
$file = ltrim($file,"/");// If the url points to a directory, or if it's the root dir, add 'index.html':
if ( (substr($file, -1) == '/') ¦¦ ( strlen($file) == 0 ) ) $file .= 'index.html';// If the file or url does not exist, raise a 404 header and return error file:
if ( !file_exists($file) ) {
header( "HTTP/1.1 404 Not Found" );
$file = "error/error404.html";
$output = file_get_contents($file);
echo $output;
exit;
}// Get the file's content:
$file_contents = file_get_contents($file);[...do gzip...]
This works fine except for the urls with parameters for which I momentarily have turned off the handler.