.htaccess重定向主机和IP地址

时间:2014-02-11 14:49:31

标签: apache .htaccess redirect http-host

您好〜这是我发给stackoverflow的第一篇文章

我们正在尝试在htaccess文件中满足以下条件:

如果您不在某个IP地址范围内(111.222.xxx.xxx)且http主机是test.com,请将用户带到test.com/goodbye:

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

如果您在某个IP地址范围内(111.222.xxx.xxx)且https主机是test.com,请将用户带到test.com/hello:

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

无论我使用哪种IP,我都会被带到/你好。我认为第一个条件是以某种方式失败了?

1 个答案:

答案 0 :(得分:1)

反转规则并使你的正则表达式更严格:

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]