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.
Steps
- Log in to cPanel and open File Manager.
- Click Settings and tick Show Hidden Files (dotfiles). Click Save.
- Open the
public_htmlfolder. - Copy
.htaccessto a backup name, for example.htaccess-backup. - Right-click
.htaccessand choose Edit. - Add the rule below at the end of the file.
- 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
- Open a command window on your computer.
- 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.
Need help? Open a support ticket.
Quick recap
- A user agent is the name tag a visitor program sends.
- Add a
RewriteCondandRewriteRuleto .htaccess to block it. - Back up the file first.
- Test with
curland check your normal site still opens.