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.

RewriteEngine On RewriteCond %{HTTP_USER_AGENT} ^.*(Baiduspider|Yandex).*$ [NC] RewriteRule .* - [R=403,L]

 

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
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!

Did you find this article helpful?

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Need More Help?

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.
}