Forum Moderators: phranque

Message Too Old, No Replies

Apache Rewrite - Ignore a Directory

apache rewrite question relating to ignoring a specific directory

         

pmmenneg

6:44 am on Mar 29, 2006 (gmt 0)

10+ Year Member



I have a rewrite rule that I am implementing that sends all requests for *.png to *.gif.

RewriteRule ^(.*).png$ [domain.com...] [L,R=301]

This rule works as advertised, but I need to modify the rule so that it works for my whole site except for directory /admin.

RewriteRule ^[^/admin](.*).png$ [domain.com...] [L,R=301]

I have tried the above, but no luck. Any ideas on what I am doing wrong? I can't seem to figure out how to exclude this one directory from this rule.

Thanks in advance,

P

jdMorgan

1:31 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See mod_rewrite's RewriteCond [httpd.apache.org] directive. Used in conjunction with either the %{REQUEST_URI} variable or with a back-reference to $1, it will make the RewriteRule conditional:

RewriteCond $1 !admin/
RewriteRule ^([^.]+)\.png$ http://www.example.com/$1.gif [L,R=301]

-or-

RewriteCond %{REQUEST_URI} !/admin/
RewriteRule ^([^.]+)\.png$ http://www.example.com/$1.gif [L,R=301]

Note that [^/admin] won't work for two reasons. First, in .htaccess, the local URL-path compared against the RewriteRule pattern never starts with "/". Therefore, your pattern would likely have matched unexpected paths.

Second "[]" defines a list of characters and not a word, so "[^word]" means, "NOT w, o, r, or d". It does not mean "NOT "word". Use the "!(word)" construct in a separate RewriteCond instead.

Note also that I changed your "(.*)" pattern to "([^.]+)", which is a much more efficient pattern, and escaped the literal period in the pattern -- otherwise, the unescaped "." means "any single character."

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

pmmenneg

4:41 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



Thank you, problem resolved. I will be sure to read up on RewriteCOnd.

p