If you want to block a specific IP address from accessing your website, you can do this by adding a few lines to your .htaccess file. This is useful if you're seeing unwanted traffic or suspicious activity from a particular IP.
Step 1. Log in to your cPanel account.
Step 2. Open the File Manager from the Files section.
Step 3. Navigate to the public_html folder or the root directory of your website.
Step 4. Locate your .htaccess file. If you don’t see it, click Settings in the top-right corner and tick Show Hidden Files (dotfiles).
Step 5. Right-click the .htaccess file and select Edit. Add the following lines to the bottom of the file:
order allow,deny
deny from IP-ADDRESS
allow from all
Replace IP-ADDRESS with the actual IP address you wish to block.
Example: To block 192.168.1.1, your code would look like this:
order allow,deny
deny from 192.168.1.1
allow from all
If blocking multiple IP addresses you will need separate lines containing a deny from x.x.x.x
Step 6. Save the changes and close the editor.
This will immediately block access to your site from the specified IP address.
Restricting Access to a Directory by IP
If you have a sensitive directory (such as /admin) and you want to ensure only your own IP address can access it, you can use the following method:
Step 1. Inside the directory you want to restrict (e.g. /public_html/admin), open or create a new .htaccess file.
Step 2. Add the following lines to the file:
order deny,allow
deny from all
allow from YOUR.IP.ADDRESS
Replace YOUR.IP.ADDRESS with your own static IP address (e.g. 203.0.113.5).
Tip: If multiple team members need access, you can add additional allow from lines for each IP.
This setup will deny access to everyone except the IP addresses you've allowed, making it perfect for protecting admin panels or sensitive directories.