HOW TO:使用htaccess将url限制为特定的IP

时间:2014-02-15 06:21:41

标签: apache .htaccess mod-rewrite redirect

我需要阻止一个看起来像www.domain.com/admin/login/的特定网址?使用htaccess和grant权限仅访问特定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的URL请求。在服务器上安装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>

6 个答案:

答案 0 :(得分:2)

此规则适用于阻止该网址:

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]

答案 1 :(得分: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]

答案 2 :(得分:1)

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

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

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

这应该为您解决问题。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

在.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]

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

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

答案 5 :(得分:0)

为什么不使用Location

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