Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  cPanel  >  Learn to Block A User Agent in An .htaccess File
Top Scroll

Learn to Block A User Agent in An .htaccess File

 2 min

If there is high traffic on your website from different IP addresses with one and the same User Agent, you can restrict them by blocking. For this, you need to edit your .htaccess file and add the below rules:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} UserAgentName [NC]
RewriteRule .* - [F,L]

Here replace UserAgentName with the actual name of the User Agent. For example, for blocking a User Agent named Textbot, you should add the below code:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Textbot [NC]
RewriteRule .* - [F,L]

Add them at the start of the .htaccess file. For blocking more than one User Agent (e.g. named SCspider, Textbot and s2bot), add the below code:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*(SCspider|Textbot|s2bot).*$ [NC]
RewriteRule .* - [F,L]

For blocking the requests from machines with missing User Agent, add the below rules in your .htaccess file:

RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteRule ^ - [F]

In this way, you can clock a User Agent in an .htaccess file.

Also Read:
How To Change Document Root Using .htaccess File?
Steps to Enable Password Protection for .htaccess

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.