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

Why Your Business Needs a DevOps Consultant for Seamless Digital Transformation

In today’s fast-paced digital world, businesses must adopt innovative approaches to remain competitive. One of the most critical strategies for achieving operational efficiency and agility is implementing…

Test Database Connection is ok or not

Laravel provides an interactive shell called Tinker, which allows you to test database connections easily. Run: Then, in the interactive shell, try the following: If your connection…

The Ultimate Guide to Hiring the Best DevOps Freelancers for Your Business

In today’s fast-paced digital landscape, businesses are increasingly relying on DevOps freelancers to optimize their software development and IT operations. With the flexibility of freelance professionals, companies…

Master DevOps Skills with the Best DevOps Training Program

In today’s fast-paced IT landscape, DevOps has become a game-changer for businesses aiming to streamline software development and operations. Organizations are increasingly adopting DevOps methodologies to enhance…

Find the Best DevOps Trainers to Accelerate Your IT Career

The demand for skilled DevOps professionals is skyrocketing as organizations strive for faster development cycles, improved collaboration, and enhanced automation. Whether you are an IT professional looking…

Boost Your IT Career with the Best DevOps Courses in 2025

In today’s fast-paced digital landscape, DevOps has become an essential practice for organizations aiming to streamline their software development and IT operations. With businesses increasingly adopting cloud…

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