Solved

mod_rewrite: Meaning of [OR] in RewriteCond

Posted on 2006-11-19
11
548 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now