To block a specific IP address using Apache's .htaccess file, follow these steps:
- Open the
.htaccessFile: Locate your.htaccessfile in your web server's root directory. - Block a Single IP Address:
- Replace
X.X.X.Xwith the actual IP address you wish to block.
- Replace
- Allow Access for a Specific IP Address:
- Replace
X.X.X.Xwith the IP you want to allow.
- Replace
- Country-wide Blocking:
- For blocking access from a specific country, use an online tool or service that generates appropriate CIDR notations for all subnets of that country. Copy and paste the generated directives into your
.htaccessfile.
- For blocking access from a specific country, use an online tool or service that generates appropriate CIDR notations for all subnets of that country. Copy and paste the generated directives into your
- Apply Changes: After making modifications, restart your Apache server to ensure the changes take effect.
To allow access only from a specific admin IP while denying others, use:
<RequireAll>
Require all denied
Require ip X.X.X.X
</RequireAll>
Add the following directives to the file under existing rules to block access from a specific IP address:
<RequireAll>
Require all granted
Require not ip X.X.X.X
</RequireAll>
Example Configuration:
To block 192.168.1.1, add:
<RequireAll>
Require all granted
Require not ip 192.168.1.1
</RequireAll>
To allow only 192.168.2.3:
<RequireAll>
Require all denied
Require ip 192.168.2.3
</RequireAll>
