【问题标题】:How to set a RewriteCond for a certain page URL如何为某个页面 URL 设置 RewriteCond
【发布时间】:2016-03-18 12:33:19
【问题描述】:

我已经实现了一个 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]

【问题讨论】:

    标签: mod-rewrite


    【解决方案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 也与模式 ^(.*)$ 匹配。

    【讨论】:

    • 完美@starkeen。经过测试,它完全按照我的需要工作。谢谢
    猜你喜欢
    • 2016-10-30
    • 2012-10-15
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多