繁体   English   中英

HOW TO:使用htaccess将网址限制为特定IP

[英]HOW TO: Restrict url to specific IP using htaccess

我需要屏蔽一个看起来像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]

现有的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>

UPDATE#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>

此规则应可用于阻止该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]
# 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]

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

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

为什么不使用Location

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM