Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Block a User Agent With .htaccess

This guide shows how to stop a bad bot or unwanted program from visiting your website, using a few lines in your .htaccess file.

Files and FTP2 min read9 steps

What is a user agent?

When a browser or program asks your site for a page, it sends a short name tag. This tag is the user agent. It says who is asking. A normal tag looks like Chrome or Firefox. A bot, which is a program that visits websites on its own, sends its own name.

Some bots are helpful, like search engine bots. Others copy your content, guess passwords or slow down your site. You can block them by their name tag.

A .htaccess file is a small settings file that gives your web server simple rules. The web server is the program that sends pages to visitors.

Warning: A mistake in .htaccess can break your site. Make a backup copy of the file before you start. Do not block names like Googlebot unless you want to disappear from Google.

Steps

  1. Log in to cPanel and open File Manager.
  2. Click Settings and tick Show Hidden Files (dotfiles). Click Save.
  3. Open the public_html folder.
  4. Copy .htaccess to a backup name, for example .htaccess-backup.
  5. Right-click .htaccess and choose Edit.
  6. Add the rule below at the end of the file.
  7. Click Save Changes.

Block one bot

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

This says: if the user agent contains "BadBot", refuse the visit. [NC] means the letter case does not matter. [F] sends a "403 Forbidden" answer.

Block several bots

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (BadBot|EvilScraper|SpamCrawler) [NC]
RewriteRule .* - [F,L]

The bar | means "or". Replace the sample names with the real names from your access logs.

Check that it works

  1. Open a command window on your computer.
  2. Run the command below, using your own domain.
curl -I -A "BadBot" https://example.com/

This pretends to be BadBot. You should see 403 Forbidden. A normal visit should still work in your browser.

Tip: User agents can be faked. Blocking by name stops simple bots only. For stronger protection, ask Hostvento support about a firewall for your plan.

Need help? Open a support ticket.

Quick recap

  • A user agent is the name tag a visitor program sends.
  • Add a RewriteCond and RewriteRule to .htaccess to block it.
  • Back up the file first.
  • Test with curl and check your normal site still opens.