Web Hosting Talk







View Full Version : URL Rewrite


Some-User
06-18-2009, 04:15 PM
Hi,

I currently have a rewrite rule for "www.domain.com/folder/*.jpg" which then links to a PHP script to determine if the file exists or not. If the file does not exist then PHP echos a not avaliable image. The images are not stored in the folder "folder" but in "folder2".

Obviously using PHP to handle images is very resource intensive.

Is there any way using mod_rewrite that you can:
Accept a url from www.domain.com/folder/.*jpg
Checks against folder2/.*jpg to see if exists
If not exist replaces with a different file
If exists load the image from folder2/*.jpg

I've tried many different cominations but to no avail.

WO-Jacob
06-21-2009, 03:11 AM
Hi,

I currently have a rewrite rule for "www.domain.com/folder/*.jpg" which then links to a PHP script to determine if the file exists or not. If the file does not exist then PHP echos a not avaliable image. The images are not stored in the folder "folder" but in "folder2".

Obviously using PHP to handle images is very resource intensive.

Is there any way using mod_rewrite that you can:
Accept a url from www.domain.com/folder/.*jpg
Checks against folder2/.*jpg to see if exists
If not exist replaces with a different file
If exists load the image from folder2/*.jpg

I've tried many different cominations but to no avail.

The only thing I can think of is to remove the requirement of using folder2.

You would set it up as:

in folder/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* http://www.example.com/no-image.jpg [R=302,L]

Short of changing how you have the folders though, I'm not sure what to recommend.

foobic
06-21-2009, 07:51 AM
Something like this (untested) may do exactly what you describe:
RewriteEngine On
RewriteCond %{REQUEST_URI} /folder1/(.*).jpg
RewriteCond folder2/%1.jpg !-f
RewriteRule .* differentfile.jpg [L]
# If the file doesn't exist in folder 2, serve a different file

RewriteCond %{REQUEST_URI} /folder1/(.*).jpg
RewriteCond folder2/%1.jpg -f
RewriteRule .* folder2/%1.jpg [L]
# But if the file does exist in folder 2, serve it

But probably a cleaner and more efficient way would be to first redirect all .jpg requests to folder2 and then catch the file-not-found on the new request:
RewriteEngine On
RewriteCond %{REQUEST_URI} /folder1/(.*).jpg
RewriteRule .* folder2/%1.jpg [L]
# All requests for .jpg in folder 1 go to folder 2

RewriteCond %{REQUEST_URI} /folder2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* differentfile.jpg [L]
# But if the file doesn't exist in folder 2, serve different file