I need to redirect the user if the SERVER_ADDR and SERVER_NAME variables don’t match.
I have the following rule in my httpd.conf file:
RewriteEngine on
RewriteCond %{SERVER_NAME} !%{SERVER_ADDR}
RewriteRule .* /myRedirectPage.html [PT]
When SERVER_NAME and SERVER_ADDR don’t match, I correctly get redirected. However, I also get redirected even when they are matching.
If I use the following condition it works for both matching and non-matching scenarios: (where 192.168.1.1 is the server IP)
RewriteCond %{SERVER_NAME} !192.168.1.1
To check if the variables matched, I used PHP’s $_SERVER['SERVER_NAME'] and $_SERVER['SERVER_ADDR'] variables.
How should I write the RewriteCond so that it does not redirect the user if SERVER_NAME and SERVER_ADDR match?
You can’t use server variables on the right side of a RewriteCond, so you are matching against the literal string
"%{SERVER_ADDR}", not the variable and thus, since your match is negated ("!"), the rule always matches.I think you can do something like this, though I’ve never worked with this particular format myself:
The idea is that you’re doing two matches – you’re matching
%{SERVER_NAME}against anything (always matches) and capturing the result, then matching%{SERVER_ADDR}against a backreference to the first match, which is the match of%{SERVER_NAME}.Again, I haven’t used a rule like that myself, just read about it, so it may need some tweaking.