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
Solved

mod_rewrite: Meaning of [OR] in RewriteCond

Posted on 2006-11-19
11
561 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
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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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