Forum Moderators: phranque

Message Too Old, No Replies

Need some help

         

concepts99

9:01 pm on Oct 10, 2009 (gmt 0)

10+ Year Member



Hello,

I need some help.

I have a directory

/pics which has full image size pictures
and
/pics/thumbs which contain thumbnails

All the thumbnail files start with t_ prefix

for it is

t_picture1.jpg
t_picture2.jpg

In the /pics (non thumbnail), it is the same file name, but without the t_ prefix

Sometimes thumbnails are missing

When they are missing, i am trying to redirect any request to the /pics full picture directory to use the picture from the /pics directory (same filename but without the t_)

The problem I am having is I am not sure how to strip out the t_ (ie: missing /pics/thumbs/t_picture1.jpg missing should point to /pics/picture1.jpg)[notice no t_ in /pics]

What I have come up with

Options +FollowSymLinks

RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics/thumbs/+[^_]\.jpg$ [domain...] [R=301, R]

using -f to come on only when the file does not exist.

I think the first part is right searching for /pics/thumbs/+ any file name which is .jpg but how I strip - 't_' out from the string $1 taking out t_

also, does the code above look correct?

thank you

TheMadScientist

9:44 pm on Oct 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Looks like you're really close:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics/thumbs/t_([^.]+)\.jpg$ http://example.com/pics/$1.jpg [R=301,L]

() = Store for back-reference.
+ = 1 or more of the preceding pattern.
[^.] = Is Not a . (dot)

So, the above matches pics/thumbs/t_ANYTHING-EXCEPT-A-DOT.jgp

[R=NUM] = Sets the Redirect, and defines it as permanent with the NUM 301.

[L] = Last. (Should always be used unless you have a specific reason to not use it.)

Personally, I would probably serve the file from the other location internally, rather than forcing the browser to make a new request:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics/thumbs/t_([^.]+)\.jpg$ /pics/$1.jpg [L]

By removing the absolute URL and the R=NUM flag, you can serve the file from the other directory 'internally' without needing the browser to make a new request from the other location.