拒绝从文件IPS访问 [英] Deny access from ips in file
问题描述
有一个拒绝在一个特定的文件访问的每个IP htaccess的命令? 说我有一个名为bans.txt(坐在旁边的我的.htaccess文件)文件,该文件包含IPS的简单列表,一是根据其他。我想拒绝访问该文件中的每个IP。有一个简单的.htaccess命令将做到这一点?是这样的:
Is there a .htaccess command that denies access to every ip in a specific file? Say I have a file called bans.txt (sitting next to my .htaccess file) that consists of a simple list of ips, one under the other. I want to deny access to every ip in that file. Is there a simple .htaccess command that will do it? Something like:
Deny from bans.txt
我一直在寻找,我不觉得有什么,但只是检查,谢谢。
I've been searching and I don't think there is, but just checking, thanks.
推荐答案
其实,有一个整洁的方式,从单纯的Apache实现这一任务。你需要使用一个称为 RewriteMap指令
Actually there is a neat way to achieve this task purely from Apache. You need to use a feature called RewriteMap
1 - 首先启用了mod_rewrite和.htaccess通过的httpd.conf
,然后把这个code在的httpd.conf
启用 RewriteMap指令
名为 ipmap
:
1 - First enable mod_rewrite and .htaccess through httpd.conf
and then put this code in your httpd.conf
to enable a RewriteMap
called ipmap
:
RewriteMap ipmap txt:/some/path/to/ipmap.txt
2 - 然后创建文本文件 /some/path/to/ipmap.txt
像这样的条目的列出所有要禁止的IP地址
192.168.0.1 1
192.168.0.4 1
10.119.35.8 1
...
...
3 - 反弹你的Apache进程(因为你已经改变了的httpd.conf
)
4 - 终于把这个code在的.htaccess
的 $ DOCUMENT_ROOT
目录下:
4 - Finally put this code in your .htaccess
under your $DOCUMENT_ROOT
directory:
RewriteEngine On
RewriteBase /
# if IP is found in ipmap then return Forbidden error
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^1$
RewriteRule ^ - [F,L]
这篇关于拒绝从文件IPS访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!