If one IP address keeps sending spam, probing your login page or overloading your site, you can block it with a short rule in .htaccess. The server then refuses requests from that address before your website loads. Use this method for a handful of addresses; for longer lists, use the IP Blocker tool in cPanel.
If you want to block an IP Address, you can add the below lines to your htaccess file.
order allow, deny
deny from IP-ADDRESS
allow from all
Replace IP-ADDRESS with the IP you want to block.
Before you block an address
Confirm the IP in your access logs and check that it is not your own. Many home and mobile connections use dynamic addresses that change, so a block may stop working or affect a different visitor later. Back up your .htaccess file first so you can undo any mistake.
Blocking several addresses
Add one deny from line for each address, placed between the order line and the allow line. You can also block a range, such as 192.0.2.0/24, but be careful: ranges can include real visitors.
Check the block
Ask someone on the blocked network to open your site, or test through a different connection. They should see a “403 Forbidden” page. Remove the line any time to restore access.
Frequently asked questions
Can I block my own IP by mistake? Yes. If you block your own address you will see a 403 error on your site. Use another connection to remove the rule, or restore your backup of .htaccess.
Is .htaccess the only way to block an IP? No. cPanel also has an IP Blocker tool in the Security section, which adds the same kind of rule for you.
Will blocking one IP stop all attacks? No. Attackers often switch addresses. Combine blocking with strong passwords, updates and a security plugin.
Related guides
Need cPanel hosting in Kenya? See KickHost hosting plans or contact our team.
Found this useful? Share it.
