Reputation: 3083
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.
Upvotes: 2
Views: 640
Reputation: 784868
Actually there is a neat way to achieve this task purely from Apache. You need to use a feature called RewriteMap
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 - Then create your text file /some/path/to/ipmap.txt
with entries like this listing all the IPs that you want to ban:
192.168.0.1 1
192.168.0.4 1
10.119.35.8 1
...
...
3 - Bounce your Apache process (since you have changed httpd.conf
)
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]
Upvotes: 2