Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond - when should I use [OR]?

         

Patrick Taylor

7:14 pm on Dec 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My .htaccess file contains something like the following:

# EXCLUDE a few URLs
RewriteCond %{REQUEST_URI}!^/example-page-1\.php$
RewriteCond %{REQUEST_URI}!^/example-page-2\.php$
RewriteCond %{REQUEST_URI}!^/example-page-3\.php$
RewriteCond %{REQUEST_URI}!^/example-page-4\.php$
RewriteCond %{REQUEST_URI}!^/example-page-5\.php$
# Use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ /[^.]+\.php\ HTTP/
RewriteRule etc etc

(The RewriteRule doesn't apply to a few excluded URLs.)

I see that [OR] is sometimes added to the end of RewriteCond (as, for example when banning a list of bots). Under what conditions should [OR] be used? I'd appreciate a pointer.

My example above works ok without.

Patrick

jdMorgan

11:18 pm on Dec 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[OR] is used when you want to have one OR another OR another condition trigger the rewriterule. Otherwise the default behaviour is 'AND' -- All of the RewriteConds have to be true in order to trigger the rule.

Typically, but not always, [OR] is used with positive-match ^patterns$ while the default AND is used with negative-match !^patterns$ such as yours.

But try actually reading the logic with "If" replacing RewriteCond -- "If request_uri NOT example_page_one AND If request_uri NOT example_page two AND If request_uri NOT example_page three, THEN apply this rule."

Jim

[edited by: jdMorgan at 11:57 pm (utc) on Dec. 7, 2007]

Patrick Taylor

10:51 am on Dec 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim. It's sinking in (slowly).

Patrick

g1smd

8:19 pm on Dec 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think this can be simplified a bit:
RewriteCond %{REQUEST_URI} !^/example-page-1\.php$
RewriteCond %{REQUEST_URI} !^/example-page-2\.php$
RewriteCond %{REQUEST_URI} !^/example-page-3\.php$
RewriteCond %{REQUEST_URI} !^/example-page-4\.php$
RewriteCond %{REQUEST_URI} !^/example-page-5\.php$

The new code would be:
RewriteCond %{REQUEST_URI} !^/example-page-(1񔘝񔿷)\.php$

Patrick Taylor

9:21 pm on Dec 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can now see why [OR] is used with positive-match ^patterns$ when all the conditions can't be matched at once, and why [OR] is not required with negative-match ^patterns$ - sometimes I think there's a direct correlation between programming skill and pure intelligence. Well, I'm sure there is, as programming is an exercise in logic. I console myself with the thought that I can often see the logic once it's been explained.

@ g1smd - thanks for the suggestion.

jdMorgan

9:47 pm on Dec 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you'd like to learn more about the underlying logic of AND and OR and NOT and how they apply to your question, do a search for "DeMorgan's Theorem."

Jim

Patrick Taylor

10:30 pm on Dec 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did (LOL)... and there was me, thinking I'd finally understood the difference between AND and OR.

Many thanks!

g1smd

7:47 pm on Dec 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> I can often see the logic once it's been explained. <<

Don't worry, jdMorgan does that to me all the time. :-)

Peter

10:26 pm on Dec 9, 2007 (gmt 0)

10+ Year Member



Jim,

As we're on the subject, please could you explain how things work if there are only some ORs (or is this undefined)? I've never been able to see a clear answer in the docs.

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

Is this ((A OR B) AND (C OR D))
or is it (A OR (B AND C) OR D)

Thank you.
Peter.

jdMorgan

10:33 pm on Dec 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is ( (A OR B) AND (C OR D) ) -- The [OR] flag is documented as a "local OR" in that it operates only between adjacent lines.

Jim

wilderness

10:49 pm on Dec 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you'd like to learn more about the underlying logic of AND and OR and NOT and how they apply to your question, do a search for "DeMorgan's Theorem."

Many thanks for this Jim.

It helped to find a longtime solution in using my desktop tool to search of my extensive local drive data.

Now, if only I could find way to implement your Msg #3523731?
I'd be rocking and rolling ;)

Don