0

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

所以 ...

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

阻止http://mydomain.com/thisdirectoryandallcontents/ * 除了我的 IP

我从另一个站点找到了一些可能有效的代码,但我没有 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]
4

1 回答 1

4

当我将页面(或部分)进行维护时,我有类似的要求并且基本相同。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。如果您愿意,您也可以将他重定向到主页,而不是将用户重定向到维护页面。
于 2014-04-30T10:44:17.253 回答