How can I block an IP address?
Want to block certain IP addresses from your website? You can do that with a .htaccess file or with a bit of PHP.
With .htaccess
Put this in the .htaccess file in the public_html folder:
<RequireAll> Require all granted Require not ip 192.0.2.10 Require not ip 198.51.100.0/24 </RequireAll>
Require all granted allows everyone. The line with 192.0.2.10 then blocks a single address, and the line with /24 blocks an entire range. Replace the example addresses with the addresses you want to block.
With PHP
<?php
$blokkeer = ['192.0.2.10', '198.51.100.20'];
if (in_array($_SERVER['REMOTE_ADDR'], $blokkeer, true)) {
http_response_code(403);
exit;
}
Put this at the top of your index.php or in a file that is loaded on every page.
Tip: dealing with attacks or spam? Then also take a look at the Web Application Firewall and Imunify360 in DirectAdmin.
See also in the DirectAdmin manual: Web Application Firewall.
Detailed explanation with screenshots: Showing and editing hidden files such as .htaccess in the DirectAdmin manual.
