How to Block Unwanted Bots Using .htaccess
Website loading speed depends not only on your server configuration but also on how much traffic your site receives from automated crawlers — such as search engine bots, analytics services, and spam bots. Excessive requests from these bots can overload your server and slow down your website.
To improve website performance and security, it’s a good idea to block unwanted bots from accessing your site.
Blocking Bots with .htaccess
One of the most effective ways to block bots is by using .htaccess directives. You can deny access to specific crawlers by filtering requests based on their User-Agent string.
Below is an example of a simple rules that block a bot identified as AhrefsBot:
Option 1
# Block AhrefsBot
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} “.*AhrefsBot.*” [OR]
RewriteRule “.*” “-” [F]
</IfModule>
Pick up a domain name you always dreamed of
Option 2
RewriteCond %{HTTP_USER_AGENT} AhrefsBot
RewriteRule (.*) – [F,L]
Why This Helps
By blocking unwanted crawlers, you can:
- Reduce unnecessary server load
- Speed up page loading times for real users
- Prevent bandwidth waste caused by spam bots
This approach is easy to implement, doesn’t require server-level access beyond the .htaccess file, and can significantly improve your site’s stability and performance.