DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

If you’re a WordPress user, then you’re probably upset with the amount of daily spam comments. Sure, there’s akismet, but here’s a little .htaccess trick to prevent spammers posting on your blog.

To achieve this recipe, simple paste the following code on your .htaccess file, located at the root of your WordPress install. Don’t forget to specify your blog url on line 4.
Remember to ALWAYS create a backup when editing the .htaccess file.

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

The above code looks for the referer (The url from where the page has been called) when the wp-comments-post.php file is accessed. If a referer exists, and if it is your blog url, the comment is allowed. Otherwise, the sapm bot is redirected and the comment will not be posted.


  1. This should come in handy. Thanks for sharing the method.

  2. Everyone hates spam. Even spammers hate spam 😀 Thanks for snippet.

  3. Hi there, I want to subscribe for this webpage to take most up-to-date updates, therefore where can i do it please assist.

  4. Hello

    It works well to stop spam. is it not a problem for your seo? i want to make sure RewriteCond %{REQUEST_METHOD} POST does not affect your seo ?
    Noobies ..

Leave a Comment

Your email address will not be published. Required fields are marked *