Forum Moderators: phranque
http://server01.example.com/images/12345/a.jpg
http://server01.example.com/images/12345/b.jpg
http://server01.example.com/images/12346/a.jpg
etc.
Now, if the image exists, simply serve the image as it is, if not then rewrite it to something like:
http://server02.example.com/images/12345/a.jpg
So, basically, the image is on server02 if it is not on server01.
How do I do this?
Thanks in advance for your help!
[edited by: jdMorgan at 4:41 pm (utc) on Sep. 17, 2008]
[edit reason] example.com [/edit]
But basically, use a RewriteCond to check REQUEST_FILENAME and act on the "!-f" token which means, "does not exist as a file." The RewriteRule pattern should be as specific as possible, so that only image filetype URLs are matched. If possible to restrict them to a certain directory path, then include that in the pattern as well. This will prevent unnecessary OS filesystem calls.
Jim
[edited by: jdMorgan at 4:40 pm (utc) on Sep. 17, 2008]
All the images are stored on one server (server02) and rsynced to server01 once an hour. But images are changing and new images are added every hour by members so if I want to serve the images from server01 they will not be available until the new ones are already rsynced.
So, basically, images are stored on one server and I want to rsync them to another server and serve them from there without too many errors. What other options do I have?
As explained in our charter, we discuss Apache stuff here, and we can help you debug your code, but this forum, with the few contributors that it has, cannot serve as a "free code-writing service" -- There are simply not enough people to post answers here to support that kind of "second full-time job." :(
Jim
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+\.(gif¦jp[eg]¦jpeg¦png))$ http://server02.example.com/$1 [R=302,L]
Jim
Ooops. that's what happens when you leave a tab open for two hours before replying.
Using a 404 page as a basis for redirection is a very old 'trick' that came into use before "mere users" were allowed to use mod_rewrite and other Apache modules on inexpensive/free servers. It was used, for example, on GeoCities back in the mid-90's. Since it was the *only* way to accomplish the goal, it was used.
But of course, no-one paid much attention to search engines back in those days; They were just on the rise, and most people used directories and bookmarks.
I can't say what other problems you might run into. But do be aware that returning a 404 followed by a redirect is a functional violation of the HTTP protocol. So be warned...
Jim
What happened when you tried that code?
Jim
That's what you want, because the image will be present at the current URL as soon as your servers "re-sync."
Jim
<?php
header('HTTP/1.1 302 Found');
header('Location: [server02.example.com'.$_SERVER['REQUEST_URI'])...]
?>
Using mod_rewrite to test for file-exists and immediately sending a 302 redirect, or using mod_rewrite to detect the missing image and *internally rewrite* the request to /redirect.php (which then sends the 302 response) are acceptable solutions which both avoid the 404 response.
Jim
[edited by: jdMorgan at 8:00 pm (utc) on Sep. 20, 2008]