How to Allow or Block a Specific IP Address for Your Website on cPanel
Sometimes, you want to block a specific IP address or restrict access to a part of your website to whitelisted IP addresses. Luckily, this is possible with the .htaccess
file!
If you want to block a large range of IP addresses, we recommend using the cPanel IP Blocker
by searching IP Blocker
on cPanel.
You can also password-protect directories, follow our guide here on this.
- Log into cPanel. If you're unsure, take a look at our Logging In To cPanel guide.
- Once logged in, head to
Files
, and thenFile Manager
.
- Navigate to the directory which you want to restrict access to.
- Once you've in the correct directory, click
+ File
at the top of the page.
- In the file name box, type in
.htaccess
. Then clickCreate New File
- Once the file has been created, right click on it, and press
Edit
.
- On the popup, click
Edit
.
- You'll see the file open, now we can put in the values we want!
Denying IP addresses or ranges
To set rules for denying a specific IP address, you can use the template below:
Order Allow,Deny
Deny from 185.195.238.255
Allow from all
185.195.238.255
and 185.195.238.254
are example IP addresses, please replace this with the IP address you want to block.If you want to block additional addresses, you can add them as shown below:
Order Allow,Deny
Deny from 185.195.238.255
Deny from 185.195.238.254
Allow from all
If you want to block a whole range of IP addresses following CIDR notation, you can do so as shown below:
Order Allow,Deny
Deny from 185.195.238.0/24
Allow from all
185.195.238.0/24
is an example IP address range, please replace this with the IP address range you want to block.You can also use wildcards for whole octets:
Order Allow,Deny
Deny from 185.195.*.*
Allow from all
185.195.*.*
is an example range. Please replace this with your own.Allowing IP addresses or ranges
Allowing IP address ranges are very similar to denying them.
To set rules for allowing a specific IP address and denying anything else, you can use the template below:
Order Deny,Allow
Deny from all
Allow from 185.195.238.255
185.195.238.255
and 185.195.238.254
are example IP addresses, please replace this with the IP address you want to allow.If you want to allow additional addresses, you can add them as shown below:
Order Deny,Allow
Deny from all
Allow from 185.195.238.255
Allow from 185.195.238.254
If you want to allow a whole range of IP addresses following CIDR notation, you can do so as shown below:
Order Deny,Allow
Deny from all
Allow from 185.195.238.0/24
185.195.238.0/24
is an example IP address range, please replace this with the IP address range you want to allow.You can also use wildcards for whole octets:
Order Deny,Allow
Deny from all
Allow from 185.195.*.*
185.195.*.*
is an example range. Please replace this with your own.Updated on: 18/11/2024
Thank you!