Use .Htaccess to prevent malicious IP attacks on websites, prohibit access to specified domain names, prohibit machine crawlers, and prohibit hotlinking

Use .Htaccess to prevent malicious IP attacks on websites, prohibit access to specified domain names, prohibit machine crawlers, and prohibit hotlinking

A few days ago, I discovered that my website was subjected to a large number of malicious and targeted scans by some IP addresses, attempting to obtain some internal configuration files and information on the website through brute force detection. I used .Htaccess to mitigate the attack, and added the following configuration to the .Htaccess file:

 order allow,deny
 deny from 180.97.106.
 allow from all

.Htaccess is a very powerful configuration file for a website. The more you understand its functions, the easier it will be for you to control your website configuration. Using .Htaccess to prohibit a certain IP from accessing a website is one of its basic functions. The above configuration is just one of the usages. Below I will summarize more usages under this related topic.

Block access to specified IP

 order allow,deny
 deny from 192.168.44.201
 deny from 224.39.163.12
 deny from 172.16.7.92
 allow from all

The above code shows how to block 3 different IP addresses from accessing the website.

Block access to specified IP segments

If you have a lot of IPs to ban and find it too troublesome to specify them one by one, here is how to ban an IP range at a time:

 order allow,deny
 deny from 192.168.
 deny from 10.0.0.
 allow from all

Block access to specified domain names

 order allow,deny
 deny from some-evil-isp.com
 deny from subdomain.another-evil-isp.com
 allow from all

The above code can block access to a website from a specific ISP.

Use .Htaccess to block bots and spiders

In China, I think the only search engines you need are Google and Baidu. Other small search engines, such as Sogou, 360, etc. can be ignored. Otherwise, the crawlers of these unimportant search engines will not only bring you no benefits, but will also kill your website. Here’s how to disable them:

 #get rid of the bad bot
 RewriteEngine on
 RewriteCond %{HTTP_USER_AGENT} ^BadBot
 RewriteRule ^(.*)$ http://go.away/

The above is to prohibit one type of crawler. If you want to prohibit multiple crawlers, you can configure it in .Htaccess like this:

 #get rid of bad bots
 RewriteEngine on
 RewriteCond %{HTTP_USER_AGENT} ^BadBot [OR]
 RewriteCond %{HTTP_USER_AGENT} ^EvilScraper [OR]
 RewriteCond %{HTTP_USER_AGENT} ^FakeUser
 RewriteRule ^(.*)$ http://go.away/

This code blocks three different crawlers at the same time. Note the “[OR]”.

Use .Htaccess to disable hotlinking

If your website is very popular, there will definitely be people who like the pictures, videos and other resources on your website. Some people will embed them directly into their pages without professional ethics, occupying or wasting your bandwidth and affecting the stability of your server. For such hotlinking behavior, it is easy to block their theft using .Htaccess, as shown below:

 RewriteEngine on
 RewriteCond %{HTTP_REFERER} ^http://.*somebadforum\.com [NC]
 RewriteRule .* - [F]

After adding the above code to .Htaccess, when somebadforum.com hotlinks to your website resources, the server will return a 403 Forbidden error, and your bandwidth will no longer be lost.

Here is how to block multiple sites:

 RewriteEngine on
 RewriteCond %{HTTP_REFERER} ^http://.*somebadforum\.com [NC,OR]
 RewriteCond %{HTTP_REFERER} ^http://.*example\.com [NC,OR]
 RewriteCond %{HTTP_REFERER} ^http://.*lastexample\.com [NC]
 RewriteRule .* - [F]

As you can see, .htaccess is a very powerful web server configuration tool. Through it, you can have rich and free control over the web server. The solution is usually very simple and elegant, and basically does not require restarting the server, that is, it takes effect immediately.

If you don't have this configuration file on your server, create one now!

For more articles on using .Htaccess files to block malicious IP attacks on websites, please click on the relevant links below

You may also be interested in:
  • Detailed method of using .htaccess to set up image hotlink protection
  • How to use .htaccess in Apache to enforce https access
  • How to make Apache 2 support .htaccess and implement directory encryption
  • APACHE supports the solution of .htaccess pseudo-silent rewrite error No input file specified
  • Apache uses .htaccess to block malicious User Agents (anti-spider)
  • .htaccess pseudo-static file in PHP
  • 2 ways to prevent websites from displaying file directory lists (htaccess)
  • .htaccess rewrite rules detailed description
  • A collection of practical configuration examples of .htaccess files in Apache servers
  • How to enable Nginx server to support .htaccess
  • Summary of basic configuration of .htaccess in Apache server
  • How to use .htaccess to implement pseudo-static URL in Apache server
  • Enable Apache support for .htaccess in Ubuntu
  • How to allow external network access and enable .htaccess for Apache2.4.x version of Wampserver local PHP server
  • How to use nginx to support .htaccess files to achieve pseudo-static
  • Apache uses .htaccess file to block wget from downloading website content
  • 21 common Apache .htaccess file configuration tips
  • Teach you how to use .htaccess in CI framework to hide index.php in url

<<:  Detailed explanation of using pt-heartbeat to monitor MySQL replication delay

>>:  How to implement a binary search tree using JavaScript

Recommend

MySQL learning notes: data engine

View the engines supported by the current databas...

Installation and use of Apache stress testing tools

1. Download Go to the Apache official website htt...

MySQL exposes Riddle vulnerability that can cause username and password leakage

The Riddle vulnerability targeting MySQL versions...

5 ways to quickly remove the blank space of Inline-Block in HTML

The inline-block property value becomes very usef...

Nginx configuration file detailed explanation and optimization suggestions guide

Table of contents 1. Overview 2. nginx.conf 1) Co...

Detailed explanation of storage engine in MySQL

MySQL storage engine overview What is a storage e...

Introduction to using data URI scheme to embed images in web pages

The data URI scheme allows us to include data in a...

MySQL slow query operation example analysis [enable, test, confirm, etc.]

This article describes the MySQL slow query opera...

Detailed explanation of the functions and usage of MySQL common storage engines

This article uses examples to illustrate the func...

A brief analysis of whether using iframe to call a page will cache the page

Recently, I have a project that requires using ifr...

How to add a certificate to docker

1. Upgrade process: sudo apt-get update Problems ...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

Analysis and solution of the problem that MySQL instance cannot be started

Table of contents Preface Scenario Analysis Summa...