Solved

mod_rewrite: Meaning of [OR] in RewriteCond

Posted on 2006-11-19
11
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 16

Accepted Solution

by:
HackneyCab earned 25 total points
ID: 17975112
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
ID: 17975114
It would be:

A and (B or C)
0
 
LVL 15

Expert Comment

by:periwinkle
ID: 17975117
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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 43

Expert Comment

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

Author Comment

by:FartingUncle
ID: 17975247
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
 
LVL 43

Expert Comment

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

Assisted Solution

by:ravenpl
ravenpl earned 50 total points
ID: 17975265
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
ID: 17975270
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
ID: 17982533
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
ID: 17982589
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
ID: 17982644
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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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