How to prevent spam comments using .htaccess file

stop-spamIf you have a blog like me, you are already fed up of spam and referral comments on your blog. If you own a wordpress powered blog, thankfully with these tips below you can reduce the spam comments to a great extent.

Primarily use the Akismet plugin that you get with wordpress. You need an API key for this plugin to function. Sign up for a free account at WordPress.org website. You get an API key with your account which you can use on unlimited blog. Akismet filters spam comments very well. There might be some good comments marked as spam as well, happens sometimes but then Akismet plugin learns from mistakes like humans lol. Another alternative would be using the SpamKarma plugin which works the same

But then akismet holding thousands of spam comments doent look good on my blog. I want to avoid spam not just save and mark it. Most of the spam bots hits the wp-comments-post.php file, the file which posts comments. Thus is we disallow all the direct traffic to this file almost 80% of your spam comments are avoided.

Doing this is simple, open up your htaccess file and add this to it.

RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post.php*
RewriteCond %{HTTP_REFERER} !.*mygeekpal.com.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$

This will:

  1. Detect when a POST is being made
  2. Check to see if the post is on wp-comments-post.php
  3. Check if the referrer is in your domain or if no referrer
  4. Send the spam-bot BACK to you domain homepage.



Related posts:

  1. How to Create a WordPress Plugin – Hello World!
  2. How to Create Post Accordions in WordPress
  3. 3 Most Important SEO Plugins for your WordPress Blog
  4. 301 Redirect: The Search Engine Friendly Way To
  5. WordPress Plugin – RSS Post Editor


The author is a small business owner himself specialising in Web Development. He runs Insight Studios which offers specialized Web Development, SEO Services and PPC Management for Small Business and SME thus ensuring growth and prosperity using the power of social media.

Share This Post

Related Articles

2 Responses to “How to prevent spam comments using .htaccess file”

  1. Atif,

    Last line is missing from the code. Kindly update.

    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} POST
    RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
    RewriteCond %{HTTP_REFERER} !.*mygeekpal.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^$
    RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

  2. Atif says:

    thanks bro

Leave a Reply

© 2012 MyGeekPal. All rights reserved. Site Admin · Entries RSS · Comments RSS
Designed by Insight Studios