Blocking or Redirecting Spammers by IP Address Using HTAccess

2010 Feb 15
Why Did Etisalat Block Flickr

Image by Za3tOoOr! via Flickr

After several months of fighting with  manual comment spam, I have decided to block all the traffic coming from India. All they do is just post useless comments everyday, which made me wonder if its their job to post spam.

Anyway, blocking people by IP is very straight forward using htaccess. I will show two ways on how we can block browsers based on their IP addresses

Blocking by IP block is not a very good idea because you will also block legitimate traffics too. Normally, you will do this in at least a week duration and then lift the restriction back to normal. 

Blocking IP and displaying 403 Forbidden
India's IP block that is spamming me non-stop is122.180.0.0. Adding the following code below to the htaccess, blocks all traffic coming from that IP block. You can also block specific IP by  writing its entire IP address. 

order allow,deny
deny from 122.180.
deny from 123.123.123.123
allow from all

Redirect Traffic to a specific page
Redirecting the traffic to a page might be useful instead of just giving them the 403 error page. What is did is to redirect all the traffic from India to a Rick Rolled YouTube page, lol. 

RewriteCond %{REMOTE_ADDR} ^122\.180\.\d+\.\d+$
RewriteRule .* http://www.youtube.com/watch?v=ZOU8GIRUd_g [R=302,L]

R=302 is a status code for temporary redirect and L stands for Last as in the last line. You can add more RewriteCond IP addresses by inserting an addtional line before the RewriteRule Line. 
Tweet this post

Related Entries

1 Comment

Hello Alfredo Sanchez,

I really appreciate your every post and the time you expand for sharing informative informations with your visitors.

And feel Sorry about those India's IP Spammer who spamming your blog.

Leave a comment


About Me


Alfredo Sanchez is an internet professional focusing on the study search engines behavior in particular. Supports Free Open Source Software and currently develops applications with it using XAMPP.

Recent Entries

Close