Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Block Bad Bots on WordPress with Cloudflare

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Bad bots can waste your server resources, skew your analytics, and even attempt to break into your WordPress site. Cloudflare sits between your site and the internet, so it can block these bots before they ever reach your server. This guide shows you how to use Cloudflare's free and paid features to stop bad bots while letting good ones (like Googlebot) through.

What You'll Need

  • A WordPress site (self-hosted, not WordPress.com)
  • A Cloudflare account (free tier works for most steps)
  • Your site's nameservers changed to Cloudflare's (this is required to use Cloudflare's proxy and security features)
  • Access to your Cloudflare dashboard and your WordPress admin area

Step-by-Step

  1. Log in to Cloudflare and select your domain. If you haven't added your site yet, follow Cloudflare's setup wizard. It will scan your DNS records and then ask you to change your nameservers at your domain registrar. This is a one-time step.
  2. Enable the proxy (orange cloud) for your DNS records. In the DNS settings, make sure the cloud icon is orange for your A and CNAME records. This routes traffic through Cloudflare and enables all the security features.
  3. Turn on Bot Fight Mode (free plan). Go to Security > Bots. Toggle on Bot Fight Mode. This automatically challenges or blocks requests that Cloudflare identifies as likely from bots. It's a good first line of defense, but it may occasionally catch legitimate bots, so monitor your analytics.
  4. Create a WAF rule to block known bad bots (all plans). Go to Security > WAF > Custom rules. Click Create rule. Name it something like "Block bad bots". In the If field, add a condition: Known Bots > equals > block. Then set the action to Block. This uses Cloudflare's managed bot database to block known malicious bots. You can also add a condition for Bot Score less than 30 (if you have Bot Management, which is paid) to catch more sophisticated bots.
  5. Block specific user agents (if you know them). If you see a specific bad bot in your logs (e.g., "AhrefsBot" if you don't want them), create another WAF rule. In the If field, add User Agent > contains > AhrefsBot, and set the action to Block. You can add multiple user agents by using OR between conditions. Be careful: blocking by user agent can be spoofed, so use this as a supplement, not a primary defense.
  6. Enable Under Attack Mode (for emergencies). If you're under a severe bot attack, you can enable Under Attack Mode from the Security > Settings page. This presents a JavaScript challenge to every visitor, which will stop most bots but also adds friction for legitimate users. Turn it off once the attack subsides.
  7. Use Cloudflare's Firewall Rules (legacy) or WAF Managed Rules. If you prefer a more granular approach, create a firewall rule with the expression: (cf.client.bot) and action Block. This blocks all requests that Cloudflare classifies as bots (including good ones like Googlebot, so you'll need to allowlist them). A better approach is to use the WAF Managed Rules and enable the "Cloudflare Bot Management" rules if you have the paid add-on.
  8. Test your rules. After creating rules, use a tool like curl from your terminal to simulate a request with a bad user agent: curl -A "BadBot" https://yourdomain.com. You should see a Cloudflare block page (403). Also check that Googlebot can still access your site by using the "Fetch as Google" tool in Google Search Console or by checking your Cloudflare firewall events for false positives.

Common Mistakes

  • Blocking all bots. If you block all bots, you'll block Google, Bing, and other search engine crawlers, which will tank your SEO. Always allowlist known good bots.
  • Using only user-agent blocking. Many bad bots spoof user agents to look like Googlebot. Relying solely on user-agent rules is ineffective.
  • Forgetting to enable the proxy. If your DNS record is grey-clouded, Cloudflare isn't in the path, and your rules won't apply.
  • Not monitoring false positives. Bot Fight Mode and aggressive WAF rules can block real users. Check your Cloudflare analytics and WordPress logs regularly.
  • Ignoring Cloudflare's firewall events. The Firewall Events log shows why a request was blocked. Review it to fine-tune your rules.

Tips

  • Start with Bot Fight Mode. It's a one-click solution that works well for most sites. You can always add more specific rules later.
  • Use Cloudflare's Bot Score (paid). If you have Bot Management, use the bot score in your WAF rules. A score below 30 is a strong indicator of a bad bot.
  • Combine with WordPress security plugins. Cloudflare handles the network level, but a plugin like Wordfence can add application-level protection (e.g., login attempt limits). They work well together.
  • Keep your Cloudflare cache enabled. Caching static content reduces the load on your server, making bot attacks less impactful.
  • Use Cloudflare's challenge action instead of block. If you're unsure about a bot, set the action to "Managed Challenge" instead of "Block". This lets real users through while stopping most bots.

Troubleshooting

Legitimate visitors are being blocked. Check the Firewall Events log to see which rule is blocking them. If it's Bot Fight Mode, you can add an allowlist rule for the specific IP or user agent. If it's a WAF rule, adjust the conditions (e.g., increase the bot score threshold).

Googlebot is being blocked. Verify that your WAF rules don't have a catch-all bot block. Add a rule to allow requests from Googlebot's IP ranges (you can find them in Google's documentation) or use the "Verified Bots" feature if available.

Bots are still getting through. Some bots are sophisticated and can mimic human behavior. Enable Bot Fight Mode, use a WAF rule with a low bot score, and consider upgrading to Bot Management for advanced detection. Also check that your WordPress site isn't being accessed directly via the server IP (bypassing Cloudflare).

Cloudflare is showing a 522 error. This means Cloudflare can't reach your server. This is not a bot issue; check your server's firewall to ensure it's not blocking Cloudflare's IP ranges, and verify your origin server is up.

Frequently Asked Questions

Will blocking bots affect my SEO?

Only if you block good bots like Googlebot. Make sure your rules allow verified search engine crawlers. Cloudflare's Bot Fight Mode allows known good bots by default.

Is Bot Fight Mode available on the free plan?

Yes, Bot Fight Mode is available on all Cloudflare plans, including the free tier. It's a simple toggle that challenges or blocks suspected bots.

What's the difference between Bot Fight Mode and a WAF rule?

Bot Fight Mode is a pre-configured feature that uses Cloudflare's heuristics. A WAF rule gives you more control, allowing you to specify conditions like user agent or bot score. You can use both.

Can I block a specific bot by IP?

Yes, you can create a WAF rule with the condition IP address equals the bot's IP. However, IPs can change, so this is best for a temporary block.

How do I know if a bot is bad?

Check your server logs or Cloudflare analytics. Bad bots often have high request rates, odd user agents, or attempt to access wp-login.php or other sensitive files. You can also use Cloudflare's Bot Score if you have Bot Management.

Will Cloudflare slow down my site?

No, Cloudflare is a CDN and typically speeds up your site by caching static content. The security checks happen at the edge, so they don't add significant latency for real users.