如何为某个页面URL设置RewriteCond

时间:2016-03-18 12:33:20

标签: mod-rewrite

我已经实现了一个mod重写条件,如下所示。基本上我希望除了我之外的所有人(来自指定的IP地址)被重定向到我网站上的某个页面。该部分有效,但这些指令最终导致无限重定向和浏览器超时。

如果页面为https://example.com/index.html,RewriteCond指令可能会如何测试页面/index.html而不再重写?

# 2016-03-18 redirect everyone except listed IPs
# 123.456.789.012 = my static Work Office address
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^(123\.456\.789\.012)$
RewriteRule ^(.*)$ https://example.com/index.html [R=302,L]

1 个答案:

答案 0 :(得分:1)

您必须将要重定向的目的地排除在:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^(123\.456\.789\.012)$
RewriteCond %{REQUEST_URI} !^/index\.html
RewriteRule ^(.*)$ https://example.com/index.html [R=302,L]

否则您将收到重定向循环错误,因为index.htm也匹配模式^(.*)$