Configuring the .htaccess file is an advanced and precise way to manage your site's security. By using the Allow and Deny directives, you can authorize or block visitors based on their IP address by following these steps:

1.Log in to your cPanel account and go to the File Manager (located in the "Files" section).

2. Enter the public_html folder. If the file does not appear, click Settings (top right corner) and enable the "Show Hidden Files (dotfiles)" option.

public.png

3. Right-click on .htaccess and select the Edit option.

hta4.png

4. Implementing Rules (Syntax)
Copy and paste the code block that best suits your needs at the end of the file:

4.1. Option A: Full Access Restriction (Authorized IPs Only)
Recommended for development environments or administrative areas.
 

apache =< 2.3  

Order Deny,Allow
Deny from all
Allow from 1.2.3.4

 
Use code with caution.
 

apache  =>2.4 

Require ip 1.2.3.4
Require ip 5.6.7.8


(Replace 1.2.3.4 with your real IP).



 
 
4.2. Option B: Selective Blocking (Deny Specific IPs)
Ideal for mitigating attacks or restricting unwanted users.
 

apache =< 2.3 


Order Allow,Deny
Allow from all
Deny from 9.10.11.12

Apache => 2.4

<RequireAll>
Require all granted
Require not ip 9.10.11.12
Require not ip 13.14.15.16
</RequireAll>





 
Use code with caution
.
 
4.3. Option C: Block by IP Ranges
You can block entire networks by omitting the last digits.
 
 

apache =< 2.3 

Order Allow,Deny
Allow from all

Block all IPs staring with 123.45
Deny from 123.45.



Apache => 2.4

Block all IPs staring with 123.45.x.x
<RequireAll>
Require all granted
Require not ip 123.45
</RequireAll>


Using CIDR Format (Example for a specifig range )
Require not ip 192.168.1.0/24


 
 
Technical Security Notes:

•  Apache Version: On servers running Apache 2.4 or higher, the standard syntax has evolved to Require ip [address].

•  File Permissions: To ensure server stability, the .htaccess file should always maintain 644 permissions.

•  Simplified Alternative: If you prefer to avoid editing code, you can use the native IP Blocker tool in the "Security" section of your cPanel, which automates this process.

Was this answer helpful? 0 Users Found This Useful (0 Votes)