【问题标题】:htaccess - how to allow multiple ips to access the sitehtaccess - 如何允许多个 ip 访问该站点
【发布时间】:2012-03-21 13:54:34
【问题描述】:

我使用此代码只允许我的 ip 访问该网站

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx
 RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintenance.html [R=302,L]
</IfModule>

现在我想允许我的朋友 ip 访问该网站。 htaccess中如何指定多个ip?

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    使用 .htaccess 文件来限制站点并添加:

    #...
    Allow from IP/SubnetMask
    Allow from IP/SubnetMask
    #...
    

    更多详情请见documentation

    【讨论】:

    • 将其放入目录或位置指令的任意行。
    【解决方案2】:

    诀窍是添加一条对允许的 IP 不执行任何操作的规则,而不是添加维护规则;因为您可以将规则标记为 Last,这意味着不会处理下一个规则(除非该规则没有修改地址,这将触发另一个通过 .htaccess 的通道)。在您的情况下,它看起来像:

    <IfModule mod_rewrite.c>
     RewriteEngine on
    
     RewriteCond %{REMOTE_ADDR} xxx\.xxx\.xxx\.xxx [OR]
     RewriteCond %{REMOTE_ADDR} xxx\.xxx\.xxx\.xxy [OR]
     RewriteCond %{REMOTE_ADDR} xxx\.xxx\.xxx\.xxz
     RewriteRule .* - [L] #do notthing
    
     #if we are here, the IP is not in the allowed list, redirect
     RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
     RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
     RewriteRule .* /maintenance.html [R=302,L]
    </IfModule>
    

    【讨论】:

      【解决方案3】:

      尝试添加另一个 RewriteCond 以及将 [OR] 标志添加到另一个 IP 条件:

      <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
       RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
       RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx [OR]
       RewriteCond %{REMOTE_ADDR} !^yyy\.yyy\.yyy\.yyy
       RewriteRule .* /maintenance.html [R=302,L]
      </IfModule>
      

      希望对您有所帮助...

      【讨论】:

      • 基本上,当你说 IP 不是 1 或 IP 不是 2 时,如果你使用 IP 1,你就不是 2,如果你使用 2,你就不是 1,所以没有这种情况条件可以满足
      • @MaximKrizhanovsky 实际上,由于您所说的,无论IP如何,条件都始终满足,因为条件被否定,它将始终返回true
      【解决方案4】:

      这是我们自 2013 年以来一直在使用的内容

      Options +FollowSymlinks
      RewriteEngine On
      RewriteBase /
      RewriteRule ^/maintenance(.+)/(.+)/ maintenance.php?id=$1&name=$2 [L]
      RewriteCond %{REQUEST_URI} !^/maintenance/index\.html?text=Coming+Back+Soon$
      RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111 [AND]
      RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.112
      RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png|woff2|ttf)$ [NC]
      RewriteRule ^(.*)$ https://www.cloudianos.com/maintenance/index.html?text=Coming+Back+Soon [R=302,L]
      

      第 6 行的 [AND] 至关重要,因为这两个 IP 地址都被否定了,因此 [OR] 对它们都不起作用。原因如下:if the IP is not 11.111.111.111 OR 11.111.111.112 将重定向您,除非您的设备使用两个 IP 请求资源(这实际上是不可能的),因为if 的两侧之一将始终为真。如果 IP 是 11.111.111.111,则声明为真,因为它不是 11.111.111.112,反之亦然。

      下面是这段代码的条件:

      1. 如果用户已经在维护页面上,请不要重定向他们
      2. 仅当 IP 不在这两个列表中时才重定向客户端(如果您使用某种 PHP 远程访问文件内容,例如使用 CURL,您可能还必须将服务器的 IP 列入白名单)
      3. 如果用户正在查看.css.js.png 等文件,请不要重定向用户(以便维护页面可能会请求使用此类文件)

      【讨论】:

        猜你喜欢
        • 2014-04-02
        • 1970-01-01
        • 2017-02-20
        • 2014-11-21
        • 2011-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多