43

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?

Baz
  • 36,440
  • 11
  • 68
  • 94
gabriel-kaam
  • 1,230
  • 1
  • 12
  • 13
  • 2
    `[OR]` is a [RewriteCond](https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond) flag, not a RewriteRule flag. – zylstra Aug 04 '18 at 19:28

2 Answers2

89

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...
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
waraker
  • 1,331
  • 11
  • 14
  • 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. – Yzmir Ramirez Jan 11 '17 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 https://stackoverflow.com/a/31572003/895077 – Roman Kruglov Jul 04 '17 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.

Boz
  • 163
  • 2
  • 12