Cloud Service >> Knowledgebase >> cPanel >> Learn to Block A User Agent in An .htaccess File
submit query

Cut Hosting Costs! Submit Query Today!

Learn to Block A User Agent in An .htaccess File

Blocking a user agent in an .htaccess file is straightforward. Here’s how to do it:

Access Your .htaccess File: Use FTP or a file manager provided by your web hosting service to locate and edit your .htaccess file in the root directory of your website.

Add the Block Rule: Insert the following lines to block a specific user agent:
Apache

.htaccess File

Replace BadBot with the actual user agent string you want to block. The [NC] flag makes the match case-insensitive, and the [F,L] flags indicate that the request should be forbidden and stop processing further rules.

Save the Changes: After adding the code, save the .htaccess file and upload it back to your server if necessary.

Test the Block: You can test the block by accessing your website with the specified user agent. Tools like browser developer tools or online user agent switchers can help you simulate the request.

Example

If you want to block a user agent called EvilScraper, your code would look like this:

apache

.htaccess File

Notes

Backup: Always make a backup of your .htaccess file before making changes.

Multiple User Agents: If you want to block multiple user agents, you can add additional RewriteCond lines for each one.

Server Configuration: Ensure that the mod_rewrite module is enabled on your server for these rules to work.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!