45

Just found this .htaccess rewrite code

RewriteEngine on
RewriteCond %{HTTP_HOST} ^my.domain.com$ [NC,OR]
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

And I was wondering what was the purpose of the "OR" flag. Already checked the doc http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags but coulnd find any infos.

Any ideas?

1

2 Answers 2

91

From http://httpd.apache.org/docs/current/mod/mod_rewrite.html

ornext|OR (or next condition) Use this to combine rule conditions with a local OR instead of the implicit AND. Typical example:

RewriteCond %{REMOTE_HOST}  ^host1  [OR]
RewriteCond %{REMOTE_HOST}  ^host2  [OR]
RewriteCond %{REMOTE_HOST}  ^host3
RewriteRule ...some special stuff for any of these hosts...
2
  • 3
    If you think of RewriteCond as a clause in a condition statement like IF and the flag [OR] as a logical || then it makes sense to a programmer. Jan 11, 2017 at 18:46
  • 16
    @YzmirRamirez it's not entirely the same, Apache's [OR] has a higher precedence than the implicit AND - and most programming languages have a higher precedence for && than || - so as the result e.g. in C a && b || c && d is evaluated as (a && b) || (c && d) while in Apache's mod_rewrite conditions it's vise versa, details here stackoverflow.com/a/31572003/895077 Jul 4, 2017 at 12:20
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.