Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite to exclude prefixed files

         

QuantumTiger

7:22 am on Apr 17, 2008 (gmt 0)

10+ Year Member



I'm using a PHP script to dynamically put a copyright watermark on my images. My current method means that all my jpgs appear to be called watermark.jpg which is not ideal.

So I thought I could use mod rewrite to allow me to apply the watermark whilst preserving the image filename - however I cannot work out how to get it to exclude certain files.

All the images have filenames with no prefix appart from my thumbnails which start t_ and my site graphics which start qt

eg
skylark.jpg - image needs watermarking
t_skylark.jpg - thumbnail does not need watermarking
qtBanner1.jpg - banner does not need watermarking

I''ve got as far as discovering that the following

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule /t_[a-z]+\.jpg$ watermark.php [QSA,NC]

will do the thumbnails only (the opposite of what I need). On my reading of regular expressions I was expecting that either

RewriteRule /!(t_[a-z]+)\.jpg$ watermark.php [QSA,NC]
or
RewriteRule /!(t_)[a-z]+\.jpg$ watermark.php [QSA,NC]

would invert that. But neither seems to do the trick. Any suggestions would be most welcome.

Thanks!

[quantumitger.org...]

g1smd

9:56 am on Apr 17, 2008 (gmt 0)

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



You need a RewriteCond line before the rule to test the filename. The following (one!) rule will only run if the condition is true.

jdMorgan

1:38 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to require that the requested local URL-path both end in ".jpg" and not start with "t_" or "qt". You can handle the former requirement in the RewriteRule, and add the latter requirement using a RewriteCond as noted above.

Something like:


RewriteCond %{REQUEST_URI} !/(t_¦qt)[^/.]+\.jpg$
RewriteRule \.jpg$ /watermark.php [NC,L]

The added bit of "[^/.]+" fanciness in the RewriteCond makes the watermarking specific to the "filename" only. That is, a URL-path such as /qtrack/widget-pic.jpg WILL be watermarked, because the "qt" is part of the directory name, not the image name.

Replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe characters.

Jim

QuantumTiger

2:40 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



Aha. Light dawns! I've spent a lot of time looking into the RewriteRule and regular expressions but none whatsoever looking at RewriteCond.

Many thanks to both of you for your help.

g1smd

1:35 am on Apr 18, 2008 (gmt 0)

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



Glad you got it sorted.

I've spent half of today writing and editing .htaccess code. Finally got it all working too.