【问题标题】:Using htaccess block access to a range of URLs except for my IP address使用 htaccess 阻止访问除我的 IP 地址之外的一系列 URL
【发布时间】:2014-06-16 03:11:21
【问题描述】:

如何阻止除我的 IP 地址以外的所有范围或 URL。

所以...

允许所有用户使用http://mydomain.com/*

http://mydomain.com/thisdirectoryandallcontents/* 屏蔽到除我的 IP 之外的所有人

我从another site 中找到了一些可能有效的代码,但我没有 htaccess 技能来适应它

Options +FollowSymlinks
RewriteEngine on

#urls to exclude
RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
RewriteCond %{REQUEST_URI} !^/images$
RewriteCond %{REQUEST_URI} !^/css$
RewriteCond %{REQUEST_URI} !^/$

#ip to allow access
RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11$

#send to root
RewriteRule .? /http://www.mydomain.com [R=302,L]

【问题讨论】:

    标签: .htaccess url ip maintenance maintenance-mode


    【解决方案1】:

    当我将我的页面(或部分)置于维护状态时,我也有类似的要求并且基本相同。 htaccess 文件的内容如下所示:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{REQUEST_URI} /thisdirectoryandallcontents
    RewriteCond %{REMOTE_ADDR} !=111.111.111.111
    RewriteRule ^.*$ /maintenance.php [R=302,L]
    

    我会根据你的要求一步一步解释:

    1. 每个用户都应该能够访问主页http://mydomain.com/ - 很酷,无需配置。
    2. 第一个 RewriteCond 检查请求是否位于文件夹 /thisdirectoryandallcontents 中。
    3. 第二个 RewriteCond 检查 IP 地址是否为 111.111.111.111 以外的任何其他地址(此处您必须填写您的 IP 地址)。
    4. 如果这两个条件都适用,我们会将用户重定向到 /maintenance.php。在这里,我通常会向用户输出一条消息,该页面当前正在维护中。您可以显示一条消息,说明用户无权访问 /thisdirectoryandallcontents。 如果您愿意,您也可以将用户重定向到主页,而不是将用户重定向到维护页面。

    【讨论】:

    • 这似乎正是我想要的,谢谢;但我该怎么做多个目录。我试过RewriteCond %{REQUEST_URI} /thisdirectoryandallcontents RewriteCond %{REQUEST_URI} /otherdirectory,但似乎没有用。
    • 是的,它不起作用,因为多个 RewriteCond 与逻辑 AND 结合,但您不会同时访问提到的两个文件夹。改用正则表达式来检查是否请求了任何一个目录。 RewriteCond %{REQUEST_URI} ^(/thisdirectoryandallcontents|/otherdirectory).*$
    • 很好,效果很好......但我认为它必须是多个 IP 地址的类似场景。你知道正确的语法吗?
    • 是的,只需将它们全部链接在括号内并用管道分隔它们:RewriteCond %{REQUEST_URI} ^(/dir1|/dir2|/dir3|/dir4|/dir5).*$
    • 是的,你可以那样做!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2011-06-01
    • 2012-12-05
    • 2018-12-19
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多