"OR" Flag in .htaccess mod_rewrite
61,271
Solution 1
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...
Solution 2
[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.
Author by
gabriel-kaam
Hey there, I'm Gabriel. I work at https://knr.paris/ and I also created https://www.modalova.fr/ . Check these out !
Updated on June 14, 2020Comments
-
gabriel-kaam almost 4 years
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?
-
Yzmir Ramirez over 7 yearsIf you think of
RewriteCond
as a clause in a condition statement likeIF
and the flag[OR]
as a logical||
then it makes sense to a programmer. -
Roman Kruglov almost 7 years@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