Home/Guides

Stop Contact Form Spam With a Cloudflare Rule Instead of reCAPTCHA

Every week a client asks us what they can use instead of reCAPTCHA. Our answer is usually not another captcha at all. It is a single Cloudflare firewall rule that sends visitors from outside your home country through an automatic browser check before they ever reach your contact form. In this short video we show the rule we use, and below are the steps to copy it into your own Cloudflare account.

On this page

Why we take reCAPTCHA off sites

Captchas are heavy. Google reCAPTCHA loads a chunk of JavaScript that adds at least a second or two to page load, and some form plugins make it worse. Contact Form 7, for example, loads reCAPTCHA on every page of the site whether or not that page has a form on it, so the whole site pays the speed cost for one contact page.

Removing the captcha makes the site faster, but spam climbs quickly once it is gone. The Cloudflare rule is how we get the speed back without drowning in junk submissions.

What the rule does

The rule matches two conditions together: the visitor is not in your country, and the visitor is not a known bot. Anyone who matches gets a JS Challenge, which is the automatic “checking your browser” screen you have probably seen on other sites. A real person waits a moment and carries on. Most spam scripts never get past it.

The “not a known bot” part is the piece you must not leave out. Cloudflare keeps a list of verified crawlers, including Google and Bing. If you challenge those, you stop search engines from crawling the site and your rankings will suffer. Keep both conditions in every version of this rule.

The rule expressions

This is the simple version for an Australian site. Swap AU for your own country code:

(not ip.geoip.country in {"AU"} and not cf.client.bot)

If you target more than one country, list them inside the braces separated by spaces, for example {"AU" "NZ"}. You can also match on continent instead of country, which gives broader coverage if you sell across Europe or North America. The rule works the same way, it just lets more traffic through unchallenged.

On the site in the video we also use several SEO tools that are not on Cloudflare’s known bots list, so the rule has extra exclusions for their user agents. Our more advanced version looks like this:

(not ip.geoip.country in {"AU"} and not cf.client.bot and not http.user_agent contains "opteo" and not http.user_agent contains "truclicks" and not http.user_agent contains "siteliner" and not http.user_agent contains "screaming" and not http.user_agent contains "surfer")

Replace those names with whatever tools hit your own site. User agent matching in Cloudflare is case-sensitive, so check how each tool actually identifies itself in your security events log and copy that spelling exactly.

Adding the rule in Cloudflare, step by step

  1. Log in to Cloudflare and select the domain.
  2. Open Security, then WAF. In the video this screen is labelled Firewall rules; newer dashboards call them Custom rules. Click the button to create a new rule.
  3. Give the rule a clear name, such as “Challenge outside AU”.
  4. Click Edit expression to switch from the field builder to the text editor, then paste in one of the expressions above.
  5. Change the country code (or codes) to suit your market.
  6. Set the action to JS Challenge.
  7. Save it. You can save as a draft first if you want to check it before deploying. Cloudflare will not let two rules share the same name, so rename it if you get an error.

The free Cloudflare plan in the video allowed five firewall rules and the paid plan more, so one rule for spam fits comfortably even on a free account.

Pick the right challenge

Cloudflare offers several actions and they behave very differently for real visitors:

  • JS Challenge: automatic. The browser runs a quick check and the visitor continues without clicking anything. This is the one we use.
  • Managed Challenge: Cloudflare decides what to show, which can include an interactive check. We avoid it for this rule.
  • Legacy CAPTCHA: the “select all the boats” image puzzle that takes several attempts because the pictures are blurry. Avoid it. It puts back the friction you removed when you took reCAPTCHA off.

Will it hurt real customers?

For a business that serves one country, very little. If a few percent of your traffic comes from overseas, those visitors see a short automatic check and then the site as normal. Nobody is blocked outright.

The thing that does break is automation. Uptime monitors, rank trackers, site crawlers and other tools that fetch pages from data centres in other countries will hit the challenge. That is why we add the user agent exclusions. After you deploy the rule, keep an eye on your tools for a day or two and add an exclusion for anything legitimate that stops working.

Quick checklist

  • Keep not cf.client.bot in the expression so Google and Bing are never challenged.
  • Set the country or continent to match where your customers actually are.
  • Exclude the user agents of SEO and monitoring tools you rely on.
  • Use JS Challenge, not Managed Challenge or Legacy CAPTCHA.
  • Remove the reCAPTCHA plugin or setting once the rule is live, so you actually get the speed benefit.

If you want to know what else is slowing your site down, request a free site audit from the WP Speed Fix homepage.

Brendan Tully, Principal Consultant at WPSpeedFix

About Brendan Tully

Hey I’m Brendan, Principal Consultant here at WPSpeedfix!

WPSpeedFix is a WordPress speed optimization and technical SEO agency. We fix slow WordPress and WooCommerce sites, get sites passing Google’s Core Web Vitals and solve the tricky technical problems other developers get stuck on.

Since we started WPSpeedFix in 2016 as an offshoot of our SEO agency we’ve optimized 5000+ websites.

Need Help? Get a FREE Site Audit

Get a free, expert review from the team that’s optimized over 5,000 WordPress & WooCommerce sites. Tell us what you need help with and we’ll tell you what’s wrong, what’s fixable, and give you a clear plan to make it fast.

“I'd absolutely recommend WP Speed Fix to anyone wanting to speed up their Wordpress website. We have struggled for years to get our busy site faster, yet within just a week of Brendan and the team working their magic, our site is now lightning fast. Very professional and quick to respond to emails... these guys know what they're doing. Thanks for all the work.”

Get Your FREE Site Audit

5.0 from 47 reviews on Google