Web Development

Reduce Comment Spam With htaccess


Comment spam is a problem with many blogs, one of the biggest is spam bots crawling sites and automatically submitting comments to your site. The below code snippet will check the HTTP_REFERER on the wp-comments-post.php, it will make sure that you can only get to this page if you are on the current domain.

Below is a snippet you can use on your .htaccess to help reduce the amount of spam coming through on your comments page on your WordPress blog.

Just add the following into your htaccess file to reduce spammers.

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

This snippet will check the the URL coming to the wp-comments-post.php file is from your own domain.

Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial


  1. SOFT SUNRISE says:

    i hv a custom site how can i block user not to spam with htacess

    1. Paul says:

      Adding the above code to your htaccess should help. If not then try this tutorial https://paulund.co.uk/no-comment-spam