Paul Sanchez
Paul Sanchez

Reputation: 2431

Htaccess-rewrite - Exclude some folders in the condition

I have a script that to place watermark on images.

I want a RewriteRule that will exclude the images inside the folders (thumb and thumbprofile). Meaning images inside this folders will not be stamped with watermark

Currently I have this

RewriteRule ^([^/thumb|^/profilethumb].*\.(gif|jp?g|png))$ watermark.php?image=$1&watermark=watermark.png [NC]

The problem is it does not put watermark on images in (thumb) which is correct but still it stamp watermark in the folder (profilethumb).

Upvotes: 0

Views: 167

Answers (3)

Olaf Dietsche
Olaf Dietsche

Reputation: 74058

You can first skip the two directories thumb and profilethumb with a RewriteRule, which doesn't substitute the URL-path, and then process all other images with a second rule

RewriteRule ^(?:thumb|profilethumb).*\.(?:gif|jpe?g|png)$ - [L,NC]
RewriteRule ^.*\.(?:gif|jpe?g|png)$ watermark.php?image=$0&watermark=watermark.png [L,NC]

Upvotes: 0

Oussama Jilal
Oussama Jilal

Reputation: 7739

Try this rule combined with the condition :

RewriteCond %{REQUEST_URI} !^/(profile)?thumb(/.*)?$ [NC]
RewriteRule ^.*\.(gif|png|jpe?g)$ watermark.php?image=$0&watermark=watermark.png [L,NC]

Upvotes: 2

Felipe Alameda A
Felipe Alameda A

Reputation: 11809

Maybe this is what you need:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !/(thumb|profilethumb) [NC]
RewriteCond %{REQUEST_URI}  !watermark\.php        [NC]
RewriteCond %{REQUEST_URI}  /([^.]+)\.(gif|jpg|jpeg|png) [NC]
RewriteRule .*  /watermark.php?image=%1.%2&watermark=watermark.png [NC,L]

I guess in /thumb and /profilethumb there are only images and to exclude both directories from the rule is enough. If there are other files, I don't think you want them to have the watermark either.

Upvotes: 1

Related Questions