How to: Deny comment posting to no referrer requests

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_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]

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.

73 Responses

Dec 02 2008 08:36

>the spam bot is redirected

So is reader that came from link in RSS or email. Or is using ad-blocker that clears referrers when moving between sites. etc

One more bad idea to deal with spam, don’t we have plenty already? :)

Dec 02 2008 08:54

@Rarst: If the reader came from rss reader or email, he’ll comme to the post, and then left a comment.
The above code only blocks people or bots who directly access the wp-comments-post.php file, not everyone not comming from your blog.

Dec 02 2008 10:54

Or, if you can’t access your .htaccess but CAN access your functions.php, add the following:

function check_referrer() {
if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == “”) {
wp_die( __(‘Please enable referrers in your browser, or, if you\’re a spammer, bugger off!’) );
}
}

add_action(‘check_comment_flood’, ‘check_referrer’);

Dec 02 2008 11:29

@jbj

Thanks for clarifying. :) I am no expert on how bots interact with page.

Still I am against using methods without control and ability to restore spammed comment.

Dec 02 2008 11:35

@Joost de Valk: Thanks a lot for that brilliant php function! I’ll probably write a recipe with that one later.

@Rarst: No problem! Anyways Akismet blocks 99% of spams, but this recipe may be useful to some people I think.

Dec 02 2008 13:30

>Anyways Akismet blocks 99% of spams

…and plenty of perfectly good comments. If my comments don’t get through at some blogs it’s almost always Akismet’s fault. And sometimes of that dumb “please enable cookies” plugin.

Dec 02 2008 14:50

@Rarst: you have cookies disabled by default then? Why?

Dec 02 2008 14:59

@Rarst: Yes, sometimes Akismet flags real comments as spam. Personally, I always checks Akismet queue before emptying it, to ensure that I’ll not delete a legitimate comment.

Browsing the web without cookies enabled by default might be hard, due to the large amount of sites who uses it. I know a guy who disable Javascript by default for security reasons (althought he’s using Firefox 3), and he have to manually allow the JS code…

Dec 02 2008 16:50

@Joost de Valk

I have them enabled. But that dumb plugin often has another opinion.

Dec 02 2008 19:04

Would this affect pingbacks and trackbacks?

Dec 02 2008 19:09

@archshrk: I don’t think so, but I might do more test about it before I can give a 100% sure answer. Maybe someone here knows the answer?

Dec 02 2008 20:08

Those are using trackback.php, WP Codex suggests to delete that file to disable them.

Dec 03 2008 00:16

I really like the Function property!

Might have to check that out on my girls blog..

I think I’ll just stick with Akismet.. It’s better to inconvenience yourself than your visitors. :)

- Brad

Dec 04 2008 20:16

That’s right, Brad!

Dec 05 2008 14:00

This one is useful against spammers who use bots and software… but then again there are those who have their own scripts / techniques

Dec 06 2008 07:11

I hate working on my htaccess, I don’t want to see another 500 error popping up, anyways, thanks for this recipe, my test site (which receives awful lot of spam as well) will use this for the mean time to see what happens.

Dec 09 2008 21:52

This method does help ALOT, I actually blogged about a number of htaccess methods that help prevent spam about a month ago: http://thenexus.tk/htaccess-reviewed/
and also found this plugin by ‘ask apache’ (http://www.askapache.com/htaccess/htaccess-plugin-blocks-spam-hackers-and-password-protects-blog.html)
This is useful for people who are not htaccess tweaking happy but can use wordpress.

Jan 22 2009 02:24

Nothing seems to be easier than seeing someone whom you can help but not helping.
I suggest we start giving it a try. Give love to the ones that need it.
God will appreciate it.

Apr 19 2009 21:09

This is a pretty dumb idea, with good intentions. It doesn’t account for (loyal) RSS referrals.

Jun 26 2009 14:31

Thanks for that.On one of my WP blogs a receive more then 50 spamm comments every day!

Nov 22 2009 05:17

Excellent option. If you have to replace the names of fields in form – an end to spam in your blogs.

Dec 17 2009 22:05

Hello to All the Guests and Members,
My computer worked not correctly, many errors. Help me, please to fix errors on my computer.
I used Win Vista.
Thanks,
Desimmege

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required