【问题标题】:HOW TO: Restrict url to specific IP using htaccess如何:使用 htaccess 将 url 限制为特定 IP
【发布时间】:2014-02-15 06:21:41
【问题描述】:

我需要阻止看起来像 www.domain.com/admin/login/ 的特定 URL?使用 htaccess 并授予仅访问特定 IP 地址的 url 的权限。

我们已经尝试过了,它部分有效。这 ”?”在 URL 中似乎有问题。当我包括 ?在 htaccess 以及如下所示的 URL 中,它不会按照所需的方式工作。我在这里做错了什么?

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=xxx\.xxx\.xxx\.xxx
RewriteRule admin/login/?$ / [R=301,L]

Existing htaccess

更新: 我有一个自定义 CMS,它没有典型的树结构,即http://www.mysite.com/folder 不对应于/var/www/folder,实际上http://www.mysite.com/folder 根本没有单个文件或目录表示。因此,/folder 没有对应的 .htaccess 文件。我看不到如何将 URL 而不是目录附加到重写规则。以下是我一直在玩的重写概念:

这行得通吗?我在网上找到了这个:

<Location /folder>
Order deny,allow
deny from all
allow from xxx.xxx.xxx.xxx
allow from xxx.xxx.xxx.xxx
</Location>

更新#2: 问题已解决。问题是由于我使用 Cloudflare 的原因。这使得所有请求都来自 cloudflare。在服务器上安装 mod_cloudflare 并使用以下脚本后,我成功地阻止了对仅允许指定 IP 的公共的访问。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)admin(.*)$
RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteRule .* / [R=302,L]
</IfModule>

【问题讨论】:

  • 获取 指令的内部服务器错误
  • 什么样的“内部错误”? &lt;Location&gt; 就是为此而设计的。

标签: apache .htaccess mod-rewrite redirect


【解决方案1】:

此规则应该适用于阻止该 URL:

RewriteEngine on

RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteRule ^admin/login/? - [F,NC]

更新:将此代码放入/admin/.htaccess

RewriteEngine on

RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteRule ^login/? - [F,NC]

【讨论】:

  • 它不工作。这 ?实际上是 URL 的一部分。我应该使用\? 来逃避这个吗?
  • 还是一样。不使用更新的答案。 URL 就是这种格式。 http://www.website.com/admin/login/?。当用户键入该 URL 时,如果他们的 IP 不允许查看它,那么他们将被带到 http://www.website.com
  • 它根本没有阻塞。
  • 是的,我正在上传快照。因为我是新来的……系统不允许我添加图像。请参阅我的答案中现有 .htaccess 的链接:)
  • 不工作。我就是这么做的。我在块上现有的 ReWriteEngine 之后粘贴了您建议的脚本。其他 IP 仍然可以访问该页面。 :(
【解决方案2】:
# only allow acces to these urls from white listed IPs
Options +FollowSymlinks
RewriteEngine on
#the urls that should be checked
RewriteCond %{REQUEST_URI} ^(/login|/wp-login.php|/wp-admin).*$
RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xxx
# or this ip
RewriteCond %{REMOTE_ADDR} !=xxx.xxx.xxx.xxx
# if not fail
RewriteRule ^.*$ / [F]

【讨论】:

    【解决方案3】:

    您需要做的就是将 .htaccess 文件添加到您要阻止其 url 的网站的根目录。

    将此代码添加到 .htaccess 文件中

    order deny,allow
    allow from (please enter the ip address here to which you want to grant access)
    deny from all
    

    这应该对你有用。

    【讨论】:

      【解决方案4】:

      ? 是一个特殊的正则表达式字符,因此您必须使用反斜杠对其进行转义:\?

      【讨论】:

      • 这是正确的吗? Options +FollowSymlinks RewriteEngine on RewriteCond %{REMOTE_ADDR} !=xxx\.xxx\.xxx\.xxx RewriteRule admin/login/\?$ / [R=301,L]
      【解决方案5】:

      在 .htaccess 文件中试试这个:

      RewriteEngine On
      RewriteCond %{REQUEST_URI} ^/admin
      RewriteCond %{REMOTE_ADDR} !=10.0.0.1
      RewriteCond %{REMOTE_ADDR} !=10.0.0.2
      RewriteCond %{REMOTE_ADDR} !=10.0.0.3
      RewriteRule ^(.*)$ - [R=403,L]
      

      如果 url 以 /admin 开头并且远程地址不是列出的三个地址之一,则以愉快的方式发送浏览器。

      参考:https://www.concrete5.org/community/forums/chat/restrict-urls-starting-with-abc-to-specific-ips-.htaccess-guru

      【讨论】:

        【解决方案6】:

        为什么不使用Location

        <Location /admin/login>
            Order deny,allow
            Deny from all
            Allow from xxx.xxx.
            Allow from yyy.yyy.
        </Location>
        

        【讨论】:

          【解决方案7】:

          使用 .htaccess 尝试以下代码并更改 IP 地址。 123.123.123.123只能访问网站,其他IP重定向到maintenance.html

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-08-21
            • 2010-10-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-27
            • 1970-01-01
            相关资源
            最近更新 更多