.htaccess FilesUpdated November 2026 for Apache 2.4. The complete, current reference is Block Bots, AI Crawlers and Exploit Scanners in THE Ultimate Htaccess.
In the digital realm, safeguarding your website against malicious entities like user agents and bots is paramount. These automated scripts or programs can wreak havoc by scraping content, launching attacks, or exploiting vulnerabilities. Fortunately, Apache's .htaccess file offers a robust solution for fortifying your website's defenses. In this comprehensive guide, we'll delve into the importance of blocking user agents and bots, explore various methods to accomplish this in .htaccess files, and provide practical examples to illustrate each technique.
User agents and bots are automated tools used to access web content. While legitimate bots index web pages for search engines or perform other benign tasks, malicious ones can compromise your website's security. From scraping content to launching DDoS attacks, these bots pose a significant threat to your online presence.
Blocking malicious user agents and bots helps mitigate several security risks, including:
.htaccessLet's explore practical methods for blocking user agents and bots in .htaccess files:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} BadBot [NC]
RewriteRule ^ - [F]
This code snippet blocks any request with a user agent containing "BadBot" (case insensitive) and returns a 403 Forbidden error.
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (BadBot|EvilBot) [NC]
RewriteRule ^ - [F]
Here, we block requests with user agents containing "BadBot" or "EvilBot", ensuring that multiple malicious bots are denied access.
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^BadBot.*$ [NC]
RewriteRule ^ - [F]
This rule blocks any request where the user agent starts with "BadBot", effectively thwarting variations of the malicious bot.
The heaviest crawlers on most sites today collect training data for AI models. Ask the polite ones in robots.txt first, then block the rest by User-Agent. robots.txt stays reachable so they can still read that they are not wanted.
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/robots.txt
RewriteCond %{HTTP_USER_AGENT} (GPTBot|ClaudeBot|CCBot|PerplexityBot|Bytespider|meta-externalagent|Amazonbot|Diffbot) [NC]
RewriteRule ^ - [F]
Whether to block them is your call: some sites want to appear in AI answers. This list goes stale quickly, so check your access log monthly. The same block without mod_rewrite, plus how to catch bots that lie about being Googlebot, is in the 2026 guide.
Stay Updated: Regularly update your blocking rules to address emerging threats and vulnerabilities.
Monitor Logs: Keep an eye on access logs to identify suspicious activity and adjust blocking rules accordingly.
Avoid Overblocking: Exercise caution to prevent blocking legitimate traffic, which could impact user experience or SEO rankings.
Rate Limiting Lives Elsewhere: .htaccess has no request counter, so it cannot rate-limit. Use server-level tools such as mod_evasive, mod_qos, fail2ban or your CDN for that.
Blocking malicious user agents and bots in .htaccess files is a crucial aspect of maintaining your website's security. By proactively identifying and thwarting unauthorized access attempts, you can safeguard your online assets and ensure a safer browsing experience for your users. With a clear understanding of the methods and best practices outlined in this guide, you can fortify your website against evolving threats and stay one step ahead of malicious actors.