How to Block AI Crawlers in nginx, Apache and Cloudflare
Originally published on webdecoy.com . robots.txt asks AI crawlers to stay away. A server rule makes them. This guide gives you working configs to block AI crawlers in nginx, Apache and Cloudflare, shows how to test each one, and covers the scrapers that hide their name. Which AI crawlers to block

Posted on Nov 22 • Originally published at webdecoy.com
robots.txt asks AI crawlers to stay away. A server rule makes them. This guide gives you working configs to block AI crawlers in nginx, Apache and Cloudflare, shows how to test each one, and covers the scrapers that hide their name.
Most sites block AI training crawlers and keep AI search crawlers, so ChatGPT search, Perplexity and Claude can still cite and link to them. The configs below use this training list:
To leave AI search as well, add OAI-SearchBot, ChatGPT-User, Claude-SearchBot, Claude-User, PerplexityBot and Perplexity-User.
Two names you will see in robots.txt guides do not belong here: Google-Extended and Applebot-Extended. They are robots.txt tokens that control AI training use, not crawlers, and they never appear in a User-Agent header. Disallow them in robots.txt.
None of these tokens match Googlebot or Bingbot, so search engines are unaffected.
Add a map in the http block, usually in /etc/nginx/nginx.conf or a file in /etc/nginx/conf.d/:
map $ http_user_agent $ block_ai_crawler { default 0; "~*(gptbot|claudebot|ccbot|bytespider|meta-externalagent|amazonbot|cohere-ai|ai2bot)" 1; } Enter fullscreen mode Exit fullscreen mode ~* makes it a case-insensitive regular expression. Then, in each server block you want to protect:
server { #...your existing config... if ( $ block_ai_crawler ) { return 403; } } Enter fullscreen mode Exit fullscreen mode Test and reload:
sudo nginx -t && sudo systemctl reload nginx Enter fullscreen mode Exit fullscreen mode The map is evaluated only when the variable is used, so it costs almost nothing per request.
Add this near the top of the.htaccess file in your site root. It needs mod_rewrite, which most hosts enable:
< IfModule mod_rewrite. c > RewriteEngine On RewriteCond % { HTTP_USER_AGENT } ( GPTBot | ClaudeBot | CCBot | Bytespider | Meta - ExternalAgent | Amazonbot | cohere - ai | AI2Bot ) [ NC ] RewriteRule. * - [ F, L ] </ IfModule > Enter fullscreen mode Exit fullscreen mode [NC] ignores case and [F] answers 403 Forbidden. On WordPress, put it above the # BEGIN WordPress block so WordPress never sees these requests.
If you manage the server config rather than.htaccess, Apache 2.4 can do the same without rewrites:
SetEnvIfNoCase User - Agent "(GPTBot|ClaudeBot|CCBot|Bytespider|Meta-ExternalAgent|Amazonbot|cohere-ai|AI2Bot)" block_ai_crawler < Location "/" > < RequireAll > Require all granted Require not env block_ai_crawler </ RequireAll > </ Location > Enter fullscreen mode Exit fullscreen mode Cloudflare If your site runs through Cloudflare, block AI crawlers at the edge so they never reach your server.
(lower(http.user_agent) contains "gptbot") or (lower(http.user_agent) contains "claudebot") or (lower(http.user_agent) contains "ccbot") or (lower(http.user_agent) contains "bytespider") or (lower(http.user_agent) contains "meta-externalagent") or (lower(http.user_agent) contains "amazonbot") or (lower(http.user_agent) contains "cohere-ai") or (lower(http.user_agent) contains "ai2bot") Enter fullscreen mode Exit fullscreen mode Set the action to Block and deploy. This works on the free plan. Use contains as shown: the regex operator matches needs a higher plan.
Cloudflare also has its own one-click setting to block AI bots, and AI Crawl Control for managing them. Both use Cloudflare's list of AI crawlers. Use the custom rule when you want the exact same list on Cloudflare and on your own servers. Our comparison with AI Crawl Control covers the differences.
Send a request with an AI crawler's User-Agent and check for 403:
curl -s -o /dev/null -w "%{http_code}\n" -A "Mozilla/5.0 (compatible; GPTBot/1.2; +https://openai.com/gptbot)" https://yoursite.com/ Enter fullscreen mode Exit fullscreen mode Then confirm normal visitors and search engines still get 200:
curl -s -o /dev/null -w "%{http_code}\n" https://yoursite.com/ curl -s -o /dev/null -w "%{http_code}\n" -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" https://yoursite.com/ Enter fullscreen mode Exit fullscreen mode Over the next days, check your access logs for 403s to AI crawlers:
grep -iE "gptbot|claudebot|ccbot" /var/log/nginx/access.log | awk '{print $9}' | sort | uniq -c Enter fullscreen mode Exit fullscreen mode Scrapers that hide their name Every rule above matches the name a crawler sends. The large AI companies send their real names, so these rules stop them. Scrapers that copy a Chrome User-Agent pass straight through, along with any crawler you haven't listed yet.