Forum Moderators: phranque

Message Too Old, No Replies

Problem: Redirect Loop

         

crimpson

5:13 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Hey

I need a script that redirects all image request to a mirror via imageredirector.php?i=<path to image> (this php script checks the mirrors every 5 mins, and redirects with header("Location: url"); function. If all mirrors are down, localhost (last mirror) should be used. The problem is the rewriterule for localhost requests:

RewriteRule ^(.+)\.(gif¦jpg¦png)$ imageredirector.php?i=$1.$2 [L]

GET /test.png -> Redirect to [localhost...] -> Redirect to [localhost...] -> Redirect to [localhost...] -> etc.... so, just an endless loop

is there any way to disable the RewriteRule if its the 2nd request for localhost?

jdMorgan

5:41 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, because after a redirect, you're dealing with an entirely new HTTP request, and the server has no "history" of previous requests.

You could test the {HTTP_HOST} variable and inhibit the rule if the hostname is localhost. Or you could 'tag' redirected requests by prepending or appending additional path information to indicate that it's already been redirected, and then strip that path information on the mirrors before serving the image.

Just remember that HTTP is a stateless protocol; The server has no memory of any previous HTTP requests. You can either change the requested URL to allow a RewriteRule to detect it for special handling, or you can use a cookie to server as a client-side 'memory'. This latter approach is what we call "sessions" in PHP.

Jim

crimpson

5:45 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Hmm... I can use echo file_get_contents() to serve the local request...

jdMorgan

6:09 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you mean by including that in redirector.php, that should work. It avoids the necessity of handling the "Oh, here we come around again" problem, and without requiring you to change the URL to 'tag' it or using the cookie...

Jim