Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

How to Restrict/Block IP Address using .htaccess File?

In the world of web development and server administration, there are times when you need to control who can access your website or web application. One common method for achieving this is by restricting or blocking specific IP addresses. By using the .htaccess file, a powerful configuration file for the Apache web server, you can easily implement IP-based access control.

Understanding the .htaccess File

The .htaccess file is a configuration file used by the Apache web server. It allows you to define rules and directives that affect the behavior of your web server. One common use case is access control, where you can specify which IP addresses are allowed to access your website or which should be denied access.

Steps to Restrict/Block IP Addresses using .htaccess

Here’s a step-by-step guide on how to restrict or block IP addresses using the .htaccess file:

1. Locate or Create the .htaccess File

First, you need to locate the .htaccess file in your web server’s document root directory. If you don’t have an .htaccess file already, you can create one using a text editor like Notepad on Windows or Nano on Linux/Unix.

2. Edit the .htaccess File

Open the .htaccess file in your text editor and add the following lines to restrict or block IP addresses.

# Block a specific IP address
Order deny,allow
Deny from 192.168.1.100

In the above example, replace 192.168.1.100 with the IP address you want to block. This rule will deny access to the specified IP address while allowing all other IP addresses.

3. Save and Test

Save the changes you made to the .htaccess file. Then, upload it to your web server’s document root directory if it’s not already there.

4. Block Multiple IP Addresses

If you want to block multiple IP addresses, you can add additional Deny from lines like this.

# Block multiple IP addresses
Order deny,allow
Deny from 192.168.1.100
Deny from 10.0.0.2
Deny from 172.16.0.10

Each Deny from line should contain the IP address you want to block.

5. Allow Specific IP Addresses

If you want to allow specific IP addresses while blocking others, you can use the following configuration.

# Allow specific IP addresses while blocking others
Order deny,allow
Deny from all
Allow from 192.168.1.100
Allow from 10.0.0.2

In this example, all IP addresses are denied by default, but Allow from lines specify the IP addresses that are allowed access.

6. Save and Reload

After making changes to the .htaccess file, save it and reload your web server. This can typically be done by restarting Apache or using a command like service apache2 restart on Linux/Unix systems.

Related Posts

Is SonarQube Community free Edition Good for Laravel Projects?

When working on web development projects using Laravel, JavaScript, and jQuery, maintaining code quality becomes just as important as building features. That’s where tools like SonarQube come…

Laravel Throttle Middleware: How to Increase API Rate Limit Safely and for 429 Too Many Requests

If you’re working with Laravel APIs, you might have encountered this default throttle setting: This line lives in your app/Http/Kernel.php file and controls how many requests a…

Fixing MySQL Error: Incorrect Definition of mysql.column_stats Table

The Problem While working on your MySQL server, you might come across this error in your error log: This error usually shows up after an upgrade or…

Fixing Laravel Migration Error: “Unknown Collation: utf8mb4_0900_ai_ci”

While working with Laravel and MySQL, you might run into an error during migrations like this one: Why This Happens The collation utf8mb4_0900_ai_ci is introduced in MySQL…

Why Dental Surgery Is Good and Important

Dental health plays a vital role in our overall well-being, yet it’s often overlooked until problems become serious. Dental surgery is a powerful solution that not only…

How to Get Cosmetic Surgery Covered by Insurance

Cosmetic surgery has become increasingly popular for individuals seeking to enhance their appearance or correct certain physical issues. While many people assume that cosmetic procedures are always…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x