【问题标题】:How to block all accesses, except three IPs, to an absolute URL with htaccess?如何使用 htaccess 阻止对绝对 URL 的所有访问(三个 IP 除外)?
【发布时间】:2015-07-14 16:19:59
【问题描述】:

如何使用 htaccess 阻止对绝对 URL 的所有访问(三个 IP 除外)?

示例数据: http://subdomain.example.com/url/i/want/to/block IP: 10.10.10.10 10.10.10.11 10.10.10.12

这是我的代码:

RewriteCond %{REQUEST_URI} http://subdomain.example.com/url/i/want/to/block
RewriteCond %{REMOTE_ADDR} !=10.10.10.10
RewriteCond %{REMOTE_ADDR} !=10.10.10.11
RewriteCond %{REMOTE_ADDR} !=10.10.10.12
RewriteRule ^.*$ /index.php [R=302,L]

它不工作。我正在使用任何 IP 访问 URL。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    您无法匹配protocolREQUEST_URI 中的端口等。这样做:

    RewriteCond %{HTTP_HOST} =subdomain.example.com
    RewriteCond %{REMOTE_ADDR} !=10.10.10.10
    RewriteCond %{REMOTE_ADDR} !=10.10.10.11
    RewriteCond %{REMOTE_ADDR} !=10.10.10.12
    RewriteRule ^url/i/want/to/block /index.php [R=302,L,NC]
    

    或使用正则表达式字符类:

    RewriteCond %{HTTP_HOST} =subdomain.example.com
    RewriteCond %{REMOTE_ADDR} !^10\.10\.10\.1[0-2]$
    RewriteRule ^url/i/want/to/block /index.php [R=302,L,NC]
    

    【讨论】:

    • 感谢您的评论。我忘了提到我的网址是一个子域。这可能就是为什么第一个解决方案允许从任何 IP 访问网站而第二个解决方案拒绝每个 IP 访问网站的原因。
    • 是的!非常感谢!
    • 不幸的是,我没有足够的声望来为您的答案投票:\
    • 您现在有足够的代表来支持任何答案。我赞成你的问题:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 2012-11-15
    • 2012-12-05
    • 2016-02-08
    相关资源
    最近更新 更多