How can I restrict access to my website using the .htaccess file in cPanel?

 
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.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to create a Cron Job?

  A cron job is simply an automated task that runs on a server at specific time intervals. You...

How to create and configure your .htaccess file?

  The .htaccess file is a powerful tool for managing your site's security and redirection....

How can I assign different PHP versions to my domains?

  If you manage multiple projects on a single account and need each domain or subdomain to run...

How can I change PHP values (limits, execution time, errors) using "MultiPHP INI Editor" (Basic Mode)?

  Here we will explain how to transcribe and adjust these values using the "MultiPHP INI Editor"...

How can I force the use of SSL (HTTPS) via the .htaccess file?

  To ensure your visitors always browse securely, you can automate the HTTPS redirection by...