Solved

mod_rewrite: Meaning of [OR] in RewriteCond

Posted on 2006-11-19
11
545 Views
Last Modified: 2012-05-05
How is the following rewrite rule interpreted by apache?

RewriteCond      A
RewriteCond      B      [OR]
RewriteCond      C
RewriteRule       X

Is it:

    (A and B) or C
    A and (B or C)
    A or B or C

or something else altogether?  How about for more complex rules.  I have not found any info on the web.
0
Comment
Question by:FartingUncle
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 16

Accepted Solution

by:
HackneyCab earned 25 total points
Comment Utility
According to this site:

    http://abates.tetrap.com/archives/2006/05/08/optimising_mod_rewrite_part_1.html

the [OR] operator has a higher precedence than the default (silent) AND operator.

So your example above should be equivalent to

A AND (B OR C)

But you'd have to test that, because you're right: there seems to be nothing on Apache's on mod_rewrite page about precedence.
0
 
LVL 15

Expert Comment

by:periwinkle
Comment Utility
It would be:

A and (B or C)
0
 
LVL 15

Expert Comment

by:periwinkle
Comment Utility
we must have been posting at the same time. B or C wouldn't be encountered unless A happened, so A must be resolved first.  If A, then it looks at B  OR C.
0
 
LVL 43

Expert Comment

by:ravenpl
Comment Utility
[OR] means ornext, therefore
A and (B or C)
0
 
LVL 1

Author Comment

by:FartingUncle
Comment Utility
OK - so what about a slightly more complex example?

RewriteCond     A
RewriteCond     B     [OR]
RewriteCond     C
RewriteCond     D
RewriteRule      X


From what you say, I would guess that that would give:

A and (B or C) and D

Is that right?

Is there an easy way to test?   I don't have root access so I can't change the debug level, and I found it hard to tell what was happening just by the result, which is why I posted here.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 43

Expert Comment

by:ravenpl
Comment Utility
Yes, it's right.
Set some conditions on QUERY_STRING, then test...
0
 
LVL 43

Assisted Solution

by:ravenpl
ravenpl earned 50 total points
Comment Utility
like
RewriteCond %{QUERY_STRING} var1
RewriteCond %{QUERY_STRING} var2 [OR]
RewriteCond %{QUERY_STRING} var3
RewriteCond %{QUERY_STRING} var4
RewriteRule .* /very_well.html

then enter various URLs in Your browser like
http://your.host.tld/original.html?var1=1&var3=1&var4=1
0
 
LVL 43

Expert Comment

by:ravenpl
Comment Utility
Of course upload two html's (static pages)
very_well.html
original.html
and verify which one is displayed in browser
0
 
LVL 1

Author Comment

by:FartingUncle
Comment Utility
I found an interesting hint at http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#InternalRuleset, which I hadn't noticed previously.  It doesn't offer quite enough information but it backs up what you've been saying and gives a little more detail about the inner workings of the process.

Am currently testing using the example suggested by ravenpl.  I will post my findings and award points when done.

This is the code I am using to test (with variants to the middle lines):

RewriteCond %{REQUEST_URI}       !^/?rewritten\.php$
RewriteCond %{QUERY_STRING} a
RewriteCond %{QUERY_STRING} b [OR]
RewriteCond %{QUERY_STRING} c
RewriteCond %{QUERY_STRING} d
RewriteRule .* /rewritten.php [L]

I then call /unchanged.php?abcd, where abcd is replaced by the letters I want to test for.

Note that despite using the [L] parameter, this (as the only rule) causes an infinite redirect, so I had to add the first condition to stop this.  Not sure why that's going on there...

Will get back to you with my findings.
0
 
LVL 1

Author Comment

by:FartingUncle
Comment Utility
OK - that didn't take long.  It looks like everything functions as expected.  The processor starts at the top and works through the conditions until it finds one that fails (in which case the rule is skipped) or reaches the end (in which case the substitution takes place).

Groups of [OR]ed lines are evaluated as a single entity, with lines that are not part of the [OR] treated as separate statements ANDed together.

For example:

RewriteCond %{QUERY_STRING} a
RewriteCond %{QUERY_STRING} b [OR]
RewriteCond %{QUERY_STRING} c
RewriteCond %{QUERY_STRING} d [OR]
RewriteCond %{QUERY_STRING} e [OR]
RewriteCond %{QUERY_STRING} f

yields:

a AND (b OR c) AND (d OR e OR f)
0
 
LVL 1

Author Comment

by:FartingUncle
Comment Utility
I gave HackneyCab 25 points and the accept for being the first of the bat with the correct answer and a very useful link.  I gave 50 points to ravenpl for good advice in helping me answer the question in the general sense, which was the second and more tricky part of my question.

Thanks for all your help.  I hope this question gets indexed well enough for other people to find!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now