Today's Saturday Snippet is inspired from a post on WPRecipes.com which I thought was really, really cool. Essentially, this should help you cut down on the amount of spam comments you see on your blog.

If you've ever seen a post with no visible comment form available to the readers that also has comments on it, you may have wondered how they got there. Most likely, these are spam comments from bots. Some bots just directly access the wp-comments-post.php file with their spam-filled information, thus totally bypassing the post that they're apparently "commenting" on.

Remember, this doesn't only occur on blogs with no comment forms, it can also happen if you allow your readers to post comments. To prevent this from happening, you simply have to add this to your .htaccess file. Remember to always backup this file before you ever tamper with it.

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

On line 4, be sure to add your blog's domain.

Let me give a brief explanation of what this does. The code looks at the current user and see's if they have a referral address when they access the wp-comments-post.php file. Normal commentators will usually have a referral from the URL of the post they are commenting on, while these spam bots will have none.

If there is no referral address, they are denied access to the wp-comments-post.php file, thus blocking the spammy comment.

If you liked this post, stay updated. Follow me on Twitter or subscribe to our RSS Feed via email.

Post Tags: ,

Related Posts

Like this post? Spread it!