apache.htaccesshttp-redirecthttp-host

.htaccess redirect with host and ip address


Hello ~ this is my first post to stackoverflow

We are trying to satisfy the following criteria in our htaccess file:

If you are not in a certain range of IP addresses (111.222.xxx.xxx) and the http host is test.com, then take the user to test.com/goodbye:

RewriteCond %{REMOTE_ADDR} !^111\.222
RewriteCond %{HTTP_HOST} ^test\.com$
RewriteRule ^.*$ http://test.com/goodbye [R=301,L]

If you are in a certain range of IP addresses (111.222.xxx.xxx) and the https host is test.com then take the user to test.com/hello:

RewriteCond %{REMOTE_ADDR} ^111\.222
RewriteCond %{HTTP_HOST} ^test\.com$
RewriteRule ^.*$ http://test.com/hello [R=301,L]

no matter which IP I use, I am taken to /hello. I assume the first condition is failing somehow?


Solution

  • Reverse the rules and make your regex more strict:

    RewriteCond %{REMOTE_ADDR} ^111\.222\.
    RewriteCond %{HTTP_HOST} ^test\.com$
    RewriteRule !^hello /hello [R=301,L]
    
    RewriteCond %{REMOTE_ADDR} !^111\.222
    RewriteCond %{HTTP_HOST} ^test\.com$
    RewriteRule !^goodbye /goodbye [R=301,L]