Welcome to WebmasterWorld Guest from 54.221.75.68

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

RewriteCond - when should I use [OR]?

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 30, 2003
posts: 932
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


[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]

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 30, 2003
posts: 932
votes: 0


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

Patrick

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

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


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$

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 30, 2003
posts: 932
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 30, 2003
posts: 932
votes: 0


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

Many thanks!

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

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


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

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

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

New User

10+ Year Member

joined:Jan 1, 2004
posts:33
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member wilderness is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 11, 2001
posts:5496
votes: 3


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

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members