Page 1 of 1

.htaccess RewriteRule only when a string doesn't exist

Posted: Thu Mar 19, 2009 2:27 pm
by mfmickle
I am new to regular expressions, so hopefully this is not a stupid question. I am trying to use a .htaccess file with a RewriteRule to identify only file names and/or paths that don't contain the string "thumb_". Right now I have an expression that can find "thumb_" in the provided string, but I need just the opposite. I need to know if string doesn't contain "thumb_". Below is the complete contents of my .htaccess file and I can't figure out how to reverse it. Any thoughts?

RewriteEngine on
RewriteRule ^(.*thumb_.*\.[jJ].*)$ watermark.php?i=$1

I want these to be returned: "img.jpg" and "gallery/img.jpg"
And I don't want these: "thumb_img.jpg" and "gallery/thumb_img.jpg"

Re: .htaccess RewriteRule only when a string doesn't exist

Posted: Thu Mar 19, 2009 2:33 pm
by prometheuzz
I know nothing about Apache's rewrite mod, but I do know a bit regex. The following regex will match any string NOT containing "thumb_":

Code: Select all

^(?:(?!thumb_).)*$

Re: .htaccess RewriteRule only when a string doesn't exist

Posted: Thu Mar 19, 2009 3:11 pm
by mfmickle
prometheuzz wrote:I know nothing about Apache's rewrite mod, but I do know a bit regex. The following regex will match any string NOT containing "thumb_":

Code: Select all

^(?:(?!thumb_).)*$
That worked...sort of. It is identifying the strings that contain "thumb_", but it's not looking for only images as my original post implied. Using your lead and further testing, I believe I have stumbled onto the solution.

Code: Select all

RewriteRule ^((?:(?!thumb_).)*\.(jpg|png).*)$ watermark.php?i=$1
Thank you so much! I pulled my hair out for two days on this and you basically solved it in six minutes.

Re: .htaccess RewriteRule only when a string doesn't exist

Posted: Thu Mar 19, 2009 3:36 pm
by prometheuzz
mfmickle wrote:...

Thank you so much! I pulled my hair out for two days on this and you basically solved it in six minutes.
You're welcome.