[英]Dynamic whitelist with htaccess allowing a form to add an IP to the whitelist?
[英]Apache dynamic IP whitelist for specific directory and files
我正在尝试将IP动态列入白名单,以授予对特定目录的访问权限。 一个php脚本将不断修改whitelist.txt文件来添加/删除条目。
我知道正确的方法是使用RewriteMap,但是我不确定如何设置它。
例如,我希望用户访问example.com时可以正常访问我的网站,但是我想拒绝所有访问块路径/目录“ http://example.com/block ”中任何内容的用户的访问权限,但这些IP除外另外,whitelist.txt中的IP地址只能访问“ block”目录中的特定文件夹和文件,例如:
http://example.com/block/123/123.txt
我已经尝试过下面的代码(这是一个粗略的草图,我确定它是完全错误的,但是只是为了理解这个想法):
RewriteEngine on
RewriteCond %{THE_REQUEST} ^\/block+\ ##apply rules only for /block directory
RewriteMap ipmap txt://var/whitelist.txt
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^\/([0-9]*).txt$ $1 [NC] ##check whitelist for matching IP AND specific dir and file
RewriteRule .* - [F,L]
当然这是行不通的。 当我访问example.com时,我的网站进入了无限重定向循环。
whitelist.txt文件如下所示:
170.172.100.162 123
152.109.211.250 43
62.55.254.83 2345
227.202.162.48 32
203.52.248.55 533
因此IP地址170.172.100.162将可以访问http://www.example.com/block/123/123.txt
IP地址152.109.211.250将有权访问http://www.example.com/block/43/43.txt
... 等等。
从您的规则开始,我已经玩了一些,并做到了:
RewriteEngine On
RewriteCond %{THE_REQUEST} \/block\/?
# apply rules only for /block directory
RewriteMap ipmap txt:/var/whitelist.txt
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^$ [NC]
RewriteRule .* /block [R=403,L]
# redirect to /block with 403 when IP address not in the whitelist
RewriteCond %{REQUEST_URI} /+[^\.]+$ [NC]
# stops when the request finds a dot '.', assuming a file
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^\d+$ [NC]
# does the redirect only when the IP is in the whitelist
RewriteRule .* /block/${ipmap:%{REMOTE_ADDR}}/${ipmap:%{REMOTE_ADDR}}.txt [R=permanent,L]
# will redirect everything from /block to /block/x/x.txt -> x = numeric value corresponding to the request IP from the whitelist.txt file
经过测试,它的工作方式如下:
希望这可以帮助。
编辑 :
编辑2 :
目前,whitelist.txt文件中具有IP的任何用户都可以访问其他用户目录。 我试图为此找到一个条件,但并未真正找到任何东西。 因此,我现在能想到的是在目录级别具有.htaccess
文件,如下所示:
deny from all
allow from 1.2.3.4
#1.2.3.4 is arbitrary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.