How to block visitors from your site via .htaccess

This article will demonstrate various ways to block unwanted site visitors via your .htaccess file. This will allow you to block things such as specific IP ranges, or even bots that may be causing issues with your site by not adhering to your robots.txt rules. Follow along below as we describe different ways to block these unwanted visitors.

Editing the .htaccess file

To be able to place the blocking code, you will need to know how to edit your .htaccess file, which is found in your cPanel File Manager. Once you have accessed the .htaccess file and opened the editor, you can then decide on which method you will use to block the visitors. Simply paste the appropriate code into the top of the .htaccess file to keep out the unwanted visitor.

Blocking by IP address

If you know a visitor's IP address, you can block that single IP address with a simply line of code known as a deny. Below is an example of denying access to an IP address of 111.222.111.222.

deny from 111.222.111.222

Blocking by IP range

You can also block an entire range of IP addresses. If you leave off the last set of numbers of the IP address (known as an octet), this will block the entire range of that octet. For example, 111.222.111.1 - 111.222.111.255 Below is a sample of the code you would place in your .htaccess file to do this.

deny 111.222.111

Blocking by user-agent

This next method is a bit more complex. It first requires that the RewriteEngine be activated, then evaluates the user-agent string of the incoming request of the visitor. In the sample code, if the words Baiduspider or Yandex (which are bots for Chinese and Russian search engines, respectively) is in the user-agent, it activates the RewriteRule line of code, in which we send them a 403 (access denied) code.

The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.

How did you find this article?

Please tell us how we can improve this article:

Email Address

Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!