Forum Moderators: phranque

Message Too Old, No Replies

url rewrite "if elseif else" file finding

if found great, if not try this, if not then give up.

         

xian

11:02 pm on Oct 2, 2006 (gmt 0)

10+ Year Member



I have an image server with directory structure:

/0/1/2/3/4/0123456789.gif
/S/O/M/E/W/SOMEWORD.gif

and thumbnails for most of my images:

/0/1/2/3/4/0123456789s.gif

I have my url rewrite working fine if the image exists:

RewriteRule ^(([A-Z0-9])([A-Z0-9])([A-Z0-9])([A-Z0-9])([A-Z0-9]).+)\.gif$ /$2/$3/$4/$5/$6/$1.gif [L]

Maybe a bit ugly, but it works. What I am trying to do is request the image:

/0123456789s.gif
if that doesnt exist, use the
/0123456789.gif
if that doesnt exist, default to
/not-found.gif

I thought I had this twice, but then when I would come back to it the next day it would not work (probably cache, eventually tears). I am comfortable playing with the regex, its just the logic that I am not getting. Would I use a chain/skip/next here?

Any help would be very much appreciated. I have been tempted to post about 2 dozen times, but really wanted to get it first! ;P

Thanks.

jdMorgan

12:52 am on Oct 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using your rule as an example:

# If thumbnail exists
RewriteCond $1s.gif -f
RewriteRule ^(([A-Z0-9])([A-Z0-9])([A-Z0-9])([A-Z0-9])([A-Z0-9])[^.]+)\.gif$ /$2/$3/$4/$5/$6/$1s.gif [L]
# Else if file exists
RewriteCond $1.gif -f
RewriteRule ^(([A-Z0-9])([A-Z0-9])([A-Z0-9])([A-Z0-9])([A-Z0-9])[^.]+)\.gif$ /$2/$3/$4/$5/$6/$1.gif [L]
# Else use default
RewriteRule ^[A-Z0-9]{5}[^.]+\.gif$ /not-found.gif [L]

Jim

xian

1:47 am on Oct 3, 2006 (gmt 0)

10+ Year Member



The problem I am having is how do I get the condition to look ahead to see if the rewritten file exists. For example:

I get a request for the thumb image via: images.server.com/0123456789s.gif
The URL rewrites to images.server.com/0/1/2/3/4/0123456789s.gif

But if that rewritten thumbnail doesnt exist, rewrite again/instead to images.server.com/0/1/2/3/4/0123456789.gif

is there a way to do this? Am I missing something from your post?

thanks.

jdMorgan

1:50 am on Oct 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is precisely the function of the two added RewriteCond directives.

See mod_rewrite documentation, RewriteCond directive, -f (exists as file) flag.

Jim

jdMorgan

1:52 am on Oct 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, note that on some server setups, you'll need to use this form, adding the path to document-root:

RewriteCond %{DOCUMENT_ROOT}$1s.gif -f

Jim

[edited by: jdMorgan at 1:52 am (utc) on Oct. 3, 2006]