Forum Moderators: phranque

Message Too Old, No Replies

2 questions: Redirect for security, [OR] associativity

.htaccess for security and how does OR work

         

cameraman

11:36 am on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Grr, that's associativity but I can't edit the title.

If I have this:
RewriteEngine on
RewriteBase /subdir
RewriteRule ^.* ../index.php [R=301,L]

Does that make files in /subdir 'as secure as' files above the document root? For example, for scripts that I only use as includes in other scripts and db user/password?

Completely different question, how does [OR] in RewriteCond parse out - is:
RewriteCond a$
RewriteCond b$ [OR]
RewriteCond c$ [OR]
RewriteRule ^d$ - [L]

The same as:
a AND (b OR c) AND d

or is it
((a AND b) OR c) AND d

I tried a test to see and it appears to me that it's the second, but I'm not sure my test was effective.

jdMorgan

1:45 pm on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteCond a$
RewriteCond b$ [OR]
RewriteCond c$ [OR]
RewriteRule ^d$ - [L]

That's broken, since RewriteConds can't be ORed with RewriteRules: The [OR] flag on the final RewriteCond is invalid (always).

If written as


RewriteCond a
RewriteCond b [OR]
RewriteCond c [OR]
RewriteCond d
RewriteRule e - [L]

where the letters a through e stand for a variable-to-pattern matches in each case, then the logic is

(e AND a AND (b OR c OR d)

That is to say that the [OR] is a local function operating between one RewriteCond and the subsequent RewriteCond. Note that the RewriteConds are not evaluated unless the RewriteRule pattern matches. Both of these facts are described in the documentation.

Occasionally, the fixed precedence and scope of the implicit [AND] and the explicit [OR] will get in your way. Two techniques that can help are to use negative logic, skipping a subsequent rule if your conditions ARE NOT met, and using a kludged-up "local AND" like this:


# If Googlebot from valid IP address range
RewriteCond %{REMOTE_ADDR}<>%{HTTP_USER_AGENT} ^66\.249(\.[0-9]{1,3}){2}<>Mozilla/5\.0\ \(compatible;\ Googlebot

Note that while I used "<>" to imply concatenation, this character sequence actually has no special meaning whatsoever, and is simply used to unambiguously demarcate the two variables and the two patterns.

Jim