How to block a User Agens 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 you should open your .htaccess file for editing and add the following rules:

123RewriteEngine OnRewriteCond %{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, what you should add is:

123RewriteEngine OnRewriteCond %{HTTP_USER_AGENT} Textbot [NC]RewriteRule .* – [F,L]

It is recommended to add them in the very beginning of the .htaccess file. If you want to block more than one User Agent (e.g. named SCspiderTextbot and s2bot), you can do that with the .htacces rules below:

123RewriteEngine OnRewriteCond %{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:

12RewriteCond %{HTTP_USER_AGENT} ^-?$RewriteRule ^ – [F]
About the Author

Sande Kennedy is the founder and Editor in Chief of SandeKennedy.co.ke & Kenyans247.co.ke He is a Kenyan-based Internetprenuer,blogger Political Activist & informer who has an interest in politics, governance, corporate-fraud and human-interest. Kindly drop me a note if you feel aggrieved on any matter that you would want to be highlighted  Twitter: @nyosake , Instagram: @itssandekennedy WhatsApp: +254791890826 Read More about me here