1

Simple redirect in .htaccess works:

#Redirect 302 / https://domain.toberedirected.com

But the conditional one doesn't. I want that if the remote user IP address is in the list then ignore the redirect and if not in the list, then make 302 redirect to another domain.

My code:

RewriteCond %{REMOTE_ADDR} !=1.1.1.1 [OR]
RewriteCond %{REMOTE_ADDR} !=2.2.2.2 [OR]
RewriteCond %{REMOTE_ADDR} !=3.3.3.3 [OR]
RewriteCond %{REMOTE_ADDR} !=4.4.4.4
RewriteRule ^(.*)$ "https://domain.toberedirected.com" [L]

But the code redirects anyway is my IP in the list or not?

2
  • What is the intended redirect? From what to what? The two rules (Redirect and RewriteRule) are performing different redirects. The Redirect is redirecting to the same URL-path on the target domain, whilst the RewriteRule is redirecting everything to the homepage only (although you are missing the trailing slash) - what is the intention?
    – MrWhite
    May 18, 2023 at 16:19
  • Just want to ask if you are wanting to redirect temporarily or permanently? Status code 302 Found "means that the URI of requested resource has been changed temporarily. Further changes in the URI might be made in the future" and 301 Moved Permanently "The URL of the requested resource has been changed permanently." HTTP response status codes - Redirection messages and RewriteRule Flags - R|redirect
    – Tim R
    May 19, 2023 at 4:07

1 Answer 1

0
RewriteCond %{REMOTE_ADDR} !=1.1.1.1 [OR]
RewriteCond %{REMOTE_ADDR} !=2.2.2.2 [OR]
RewriteCond %{REMOTE_ADDR} !=3.3.3.3 [OR]
RewriteCond %{REMOTE_ADDR} !=4.4.4.4
RewriteRule ^(.*)$ "https://domain.toberedirected.com" [L]

You have the logic wrong... you should be (implicitly) AND'ing the negated conditions, not OR'ing them. If you OR them then one of the conditions will always be successful (the REMOTE_ADDR will always not be one of the stated IP addresses since it can not be all of them) and so the group of OR'd conditions will always be successful and the redirect will always occur.

"AND" is the default, so you should simply remove the flags argument on these conditions. For example:

RewriteCond %{REMOTE_ADDR} !=1.1.1.1
RewriteCond %{REMOTE_ADDR} !=2.2.2.2
RewriteCond %{REMOTE_ADDR} !=3.3.3.3
RewriteCond %{REMOTE_ADDR} !=4.4.4.4
RewriteRule (.*) https://domain.toberedirected.com/$1 [R,L]

It now reads... if the REMOTE_ADDR is not 1.1.1.1 AND is not 2.2.2.2 AND is not 3.3.3.3 AND is not 4.4.4.4 then redirect...

You should really be explicit with the redirect and include the R flag.

Note sure of the intention of the redirect, but I included the $1 backreference in the substitution string in order to redirect to the same URL-path on the target. (Otherwise, why capture the URL-path in the RewriteRule pattern?) This then behaves the same as the earlier Redirect directive you had (which is "prefix-matching").


If, however, you wanted to redirect everything to the homepage on the target domain (as your original RewriteRule would have done) then change the RewriteRule directive like so:

RewriteRule ^ https://domain.toberedirected.com/ [R,L]

Note that the RewriteRule pattern (^) does not match/capture anything, but is successful for everything. And the inclusion of the trailing slash after the hostname in the substitution string.

However, this many-to-one redirect is generally not good for SEO/UX since search engines are likely to see it as a soft-404 and users end up on a page they are not expecting and bounce.


UPDATE: I've just noticed you had previously tagged the question "wordpress". If this is WordPress then the above redirect must go at the very top of the .htaccess file, before the WordPress code block (ie. before the # BEGIN WordPress comment marker).

1
  • 1
    Big thanks! It worked. Logic needs to be further learned.
    – user109077
    May 18, 2023 at 18:26

Your Answer

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

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