I try to give access to two of my IP's to a page on my site in the .htaccess but it's a issue with my if statment that just the first IP checks and never checks the secound IP. My code is:
<If "%{REMOTE_ADDR} != '123.123.123.123'" AND "%{REMOTE_ADDR} != '456.456.456.456'">
RedirectMatch ^/mypage$ example.com [R=302,L]
</If>
I have tested with &&
insted for AND
but it not work. What is wrong with this code?
CodePudding user response:
<If "%{REMOTE_ADDR} != '123.123.123.123'" AND "%{REMOTE_ADDR} != '456.456.456.456'"> RedirectMatch ^/mypage$ example.com [R=302,L] </If>
You have multiple syntax errors here:
As you pointed out, you need to use
&&
, notAND
in the expression. There is no operatorAND
.The double quotes
"
should surround the entire expression. You essentially have two separate expressions, which is not supported.The
RedirectMatch
mod_alias directive is invalid. The 3rd (flags) argument[R=302,L]
is part of the mod_rewriteRewriteRule
directive and is not permitted here. The target URL (2nd argument) needs to be either a root-relative or absolute URL.
This should be written like this instead:
<If "%{REMOTE_ADDR} != '123.123.123.123' && %{REMOTE_ADDR} != '456.456.456.456'">
RedirectMatch 302 ^/mypage$ https://example.com/
</If>
However, I would consider using mod_rewrite for this instead. For example:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=123.123.123.123
RewriteCond %{REMOTE_ADDR} !=456.456.456.456
RewriteRule ^mypage$ https://example.com/ [R=302,L]
NB: There is no slash prefix on the URL-path matched by the RewriteRule
pattern.
If you are intending to "block" access, consider sending a 403 Forbidden, instead of redirecting. For example:
:
RewriteRule ^mypage$ - [F]
Or, if using RedirectMatch
inside the <If>
expression:
RedirectMatch 403 ^/mypage$