GwM
GwM

Reputation: 21

Create new .htaccess file to deny

Let me start by saying I am new to creating an htaccess file. I want to deny certain IPs from access to our site. I created this and I am looking for validation that this is correct. I know that there is no advanced redirect page within this as I am not sure how to accomplish that yet. I am more concerned that this snippet would work to block IPs. Thanks in advance for any and all help.

#.htaccess     
DirectoryIndex index.htm    
#deny list    
order allow,deny    
allow from all    
deny from xxx.xxx.xxx.xxx    
deny from yyy.yyy.yyy.yyy

Upvotes: 2

Views: 173

Answers (1)

wp78de
wp78de

Reputation: 18950

Looks good to me, assuming you're on Apache 2.2 To block individual visitors, you can use the following directives:

Order Allow,Deny
Allow from all
Deny from 123.123.123.123

Instead of blocking visitors, you can redirect them to another location. Here's how to do it using Apache's mod_rewrite:

#<IfModule mod_rewrite.c>
    RewriteCond %{REMOTE_ADDR} ^123\.123\.123\.123$
    RewriteRule .* https://www.google.com [R=301,L]
#</IfModule>

See also: https://htaccessbook.com/block-ip-address/

Alternatively, try this to block a range if IPS (here 10.0.8.0-10.0.8.21:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^10\.0\.8\.(2[01]|[0-9][0-9]|[0-9])
#or RewriteCond %{HTTP_HOST} 12\.34\.56\.789
RewriteRule .* https://www.google.com [L,R=301]

If you are on Apache 2.4 this link from the htaccess book shows the differences between 2.2 and 2.4: https://htaccessbook.com/access-control-apache-2-4/

Upvotes: 1

Related Questions