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

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

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

How to Generate a GitHub OAuth Token with Read/Write Permissions for Private Repositories

When working with GitHub, you may need to interact with private repositories. For that, GitHub uses OAuth tokens to authenticate and authorize your access to these repositories….

Laravel Error: Target class [DatabaseSeeder] does not exist – Solved for Laravel 10+

If you’re working with Laravel 10+ and run into the frustrating error: …you’re not alone. This is a common issue developers face, especially when upgrading from older…

JWT (JSON Web Token) vs OAuth 2.0

Both JWT and OAuth 2.0 are used for managing authentication and authorization, but they serve different purposes and work in distinct ways. 1. Purpose: 2. Role: 3….

Exploring and Creating a Proof of Concept (POC) to Upload APK Directly from GitHub Package

Automating the process of uploading an APK (or AAB) to the Google Play Store from GitHub can significantly speed up your CI/CD pipeline. By integrating Google Play’s…

A Detailed Guide to CI/CD with GitHub Actions

Continuous Integration (CI) and Continuous Deployment (CD) are modern software development practices that automate the process of integrating code changes, running tests, and deploying applications. With the…

Step-by-Step Guide for Setting Up Internal Testing in Google Play Console

1. Understanding the Types of Testing Before uploading your Android app for internal testing, it’s essential to know the differences between the testing options available in Google…

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