Snippets

Block/reduce WordPress spam comments via .htaccess

Akismet key is a useful tool to prevent spam comments, however, it is not a possible solution for every WP site, so another easy (cut n paste) solution is this htaccess snippet that blocks spam comments.
Most spammers attack your site form “wp-comments-post.php”, at the same time you prevent extra load on your server.

Paste this code into your .htaccess file.

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

Share this snippet

 

Comments (2) - Leave yours

  1. Geoffrey said:

    I think with this one more line, just before the first http_referer condition, it’s better :

    RewriteCond %{HTTP_REFERER} !^$

    So, the final code could be :

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

    I’m not sure but I use it on my own blog, and it works …

Leave a Reply

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

Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you!