Blocking or Redirecting Spammers by IP Address Using HTAccess

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 i 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.

Written by tildemark

Alfredo Sanchez, Jr is an internet technologist who spent so little time away from his computer. This little time is usually spent on teaching college students at a nearby university. He's been writing technology topics since 2005 and if combined might be able to make a book out of it.

Comments

8 Comments on "Blocking or Redirecting Spammers by IP Address Using HTAccess"

  1. 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.

  2. masini says:

    Yes, you can do this thing but maybe no all are spammers and that is really bad. You may lose important visitors and maybe clients to. I really don t know if it is good.

  3. Adam says:

    I’ve been an SEO for about a year and a half now and I’m horrified by how exposed people’s blogs are -especially those in the online industry!
    I have a 1 day approval window on my own blog. After that all comments have to be manually approved. Plus I get an email notification of all comments.
    I would suggest doing the same if you want to keep your site spam free.

  4. tildemark says:

    @Adam manual moderation will only be good if the comment count is less than 20, while i get more than 300 a day and only one is legit.

  5. I think you must be right, that people must be sitting in paid jobs spamming away. Every effort must always be made to block the spammers. And thank you for your very useful info. I have just started my own blog and am dreading the spam!
    irishpoetry

  6. Patents USA
    Thank you Alfredo Sanchez, Your post are very usefull to all visiters…
    Thank you….
    Patents USA

  7. zar says:

    Is it possible to use that to my blog? I want to block this person who kept on visiting my blog and leaving me nasty comment. I was just wondering if I can block his ip address.

  8. Hi Alfredo, thanks for the post.

    Is there a quick way to ‘convert’ a normal IP to the above standard?

    I have a list of Indian IP ranges (900+) but to change them by hand to ^123\.123.\d+\.\d+$ … would take forever!

    Thanks in advance.


Here's your chance to leave a comment!