Forum Moderators: phranque

Message Too Old, No Replies

How to use RewriteCond to ignore some folders

Do not apply the follwing RewriteRules to these folders

         

iProgram

4:21 am on Nov 3, 2004 (gmt 0)

10+ Year Member



My website looks like this:
-public_html
-\folder1\
-\folder1\.htaccess
-\images\
-\js\
-index.html
-test.php
-.htaccess
And here is the .htaccess file in root folder:

RewriteRule ^(.*)/$ test1.php?url=$1 [L]
RewriteRule ^(.*)/(.*)/$ test2.php?url=$1&id=$2 [L]
....

This will rewrite domain.com/abc/ to domain.com/test1.php?url=abc

But I want to apply another rewrite rule to \folder1\.htaccess, who can I ignore this folder in the root .htaccess file? Perhaps I need to add some lines like RewriteCond?

jdMorgan

8:05 pm on Nov 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code will not do what you expect, because the first rule will match anything the second rule might match.

RewriteRule ^(.*)/$ test1.php?url=$1 [L]
RewriteRule ^(.*)/(.*)/$ test2.php?url=$1&id=$2 [L]

Remember that the pattern ".*" will match any number of any characters, including the slash that separates your two directory levels. Therefore, the second rule will never be evaluated or run, and test2.php will never be activated.

In addition, using ".*" can be very inefficient when two or more such subpatterns are present, as mod_rewrite will have to evaluate the string from both ends to figure out how to divide the string between the two subpatterns.

I'd suggest:


RewriteRule ^([^/]+)/$ test1.php?url=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ test2.php?url=$1&id=$2 [L]

where the pattern "[^/]+/" matches at least one (or more than one) character that is not a slash, followed by a slash. This pattern is much more specific, and faster to process.

The simple solution to /folder1/.htaccess is to put the rule for /folder1/.htaccess before the rules above, and use a positive-logic RewriteCond:


RewriteCond %{REQUEST_URI} ^/folder1/\.htaccess
RewriteRule <whatever your rule is> [L]
<code for test1 and test2 follows>

Alternatively, you could qualify each of the existing rules with a negative RewriteCond:

RewriteCond %{REQUEST_URI} !^/folder1/\.htaccess
RewriteRule ^([^/]+)/$ test1.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^/folder1/\.htaccess
RewriteRule ^([^/]+)/([^/]+)/$ test2.php?url=$1&id=$2 [L]

or use the "skip" function of mod_rewrite to skip those two rules if /folder1/ is requested:

RewriteRule ^folder1/\.htaccess - [S=2]
RewriteRule ^([^/]+)/$ test1.php?url=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ test2.php?url=$1&id=$2 [L]

There are very probably several other ways to solve the problem. This just illustrates two that will work for any purpose. The difference betweent these methods is maintainability versus efficiency; The second version will be much more efficient if you want to skip a large number of rules, while the first version is easier to understand and maintain (because you don't have to re-count the rules to be skipped if you modify the code).

Jim

iProgram

3:39 am on Nov 4, 2004 (gmt 0)

10+ Year Member



Dear moderator I have to bookmark this page and test test test...