How to block User Agents in an .htaccess file?
In case there is too much traffic towards your website coming from different IP addresses with one and the same User Agent, the easiest option to restrict them is to block the User Agent. For that purpose open your .htaccess file for editing and add the following rules:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} UserAgentName [NC]
RewriteRule .* - [F,L]
where you should substitute UserAgentName with the actual name of the User Agent. For example, if you want to block a User Agent named Textbot, add it as:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Textbot [NC]
RewriteRule .* - [F,L]
It is recommended to add them in the very beginning of the .htaccess file. To block more than one User Agent (e.g. named SCspider, Textbot, and s2bot), do that with the .htacces rules below:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*(SCspider|Textbot|s2bot).*$ [NC]
RewriteRule .* - [F,L]
To block the requests from machines with missing User Agent, add the following rules in your .htaccess file:
RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteRule ^ - [F]