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

Fixing the “Could not find PHP executable” Error in Live Server on VS Code

this is a common issue and easy to fix! This guide will walk you through the step-by-step solution to get your PHP files running in the browser….

How to Fix the “npm.ps1 cannot be loaded” Error on Windows When Running npm start

If you’re a developer working with React or any Node.js-based projects, you may have encountered the following error when trying to run npm start in PowerShell on…

Simplify Database Migrations with kitloong/laravel-migrations-generator in Laravel

Laravel provides a powerful migration system that allows developers to easily define and manage database schema changes. However, when working with legacy databases or large projects, manually…

Understanding and Fixing the “Unable to Read Key from File” Error in Laravel Passport

Laravel Passport is a powerful package for handling OAuth2 authentication in Laravel applications. It allows you to authenticate API requests with secure access tokens. However, like any…

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…

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