Forum Moderators: phranque

Message Too Old, No Replies

ModRewrite if filename does NOT start with a letter

         

tekneck

7:38 am on Dec 17, 2009 (gmt 0)

10+ Year Member



I am using a on-the-fly image watermarking script and don't want to call it if the image being requested is a thumbnail.

Thumbs always start with the letter t...

my_images/username/album_id/image1.jpg needs to be rewritten but...
my_images/username/album_id/timage1.jpg must not be.

How do I test for this?

I know below is wrong... but you can see what I am trying to do...

RewriteCond %{REQUEST_FILENAME}
RewriteRule .!^t*\.(jpe?g¦gif¦bmp¦png)$ myscript.php?src=%{REQUEST_FILENAME} [L]

Thanks gurus.

g1smd

9:40 am on Dec 17, 2009 (gmt 0)

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



Requirements:

- Call the script only if it is an image being requested.
- Do not watermark for images where final part of URL begins t-.
- Question: are all the images in one folder, or are their multiple branches and/or multiple levels?

Personally, I'd have thumbnails in one folder and main images in another.

Are the main images numbered? If so, it is easy to differentiate 234567.jpg and t234567.jpg. If named and not numbered, I would be cautious in using just 'starts with t' as a criteria. I'd use 't-' to be sure the file really is a thumbnail.

What other requirements are there?

jdMorgan

1:57 pm on Dec 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Rewrite if final requested URL-path-part starts with letter except for 't', contains zero or more additional
# letters, followed by one or more numbers, and ends with image filetype, preserving directory-path (if any).
RewriteRule ^(([^/]+/)*[a-su-z][a-z]*[0-9]+\.(jpe?g¦gif¦bmp¦png))$ myscript.php?src=$1 [L]

However, as g1smd points out, you will never be able to watermark an image with a name like "townhouse123.jpg" because it starts with a 't'. Better to demarcate the t/no-t boundary with a hyphen or some other non-alphanumeric character which won't match the "all-letters" requirement of the pattern:

# Rewrite if final requested URL-path-part starts with all-letters, followed by one or more numbers, and
# ends with image filetype, preserving directory-path (if any). Note that "t-" thumbnails are excluded.
RewriteRule ^(([^/]+/)*[a-z]+[0-9]+\.(jpe?g¦gif¦bmp¦png))$ myscript.php?src=$1 [L]

... and even better to be explicit about the path being an 'album', although the pattern shown below is literal, since I don't what format you're actually using to name them:

# Rewrite if final requested URL-path-part starts with "album_id", followed by all-letters,
# followed by one or more numbers, and ends with image filetype, preserving additional
# directory-path (if any). Note that "t-" thumbnails are excluded.
RewriteRule ^(([^/]+/)*album_id/[a-z]+[0-9]+\.(jpe?g¦gif¦bmp¦png))$ myscript.php?src=$1 [L]

None of these are likely to be 'just perfect' for your actual needs, though. Note that it's best to be very, very specific in designing the URLs, creating the regular-expressions patterns, and specifying your requirements in forum posts... :) It is a good and productive practice to adopt the 'style' of the comments in the code above in order to specify *exacly* what the pattern will match, and so specify exactly which kinds of URLs will be rewritten (and which kinds will not be).

If you're accepting user-generated content, then the same pattern and URL-specification can (and should) be used in 'screening' user-created filenames, so that you won't accept a user-created filename that cannot be properly rewritten.

Replace all broken pipe "¦" characters in code found on this forum with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim