How to block unwanted hosts in .htaccess?

Member

by shyann , in category: Third Party Scripts , 4 months ago

How to block unwanted hosts in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 4 months ago

@shyann 

To block unwanted hosts in .htaccess, you can use the following directives:

  1. Create a new .htaccess file in the root directory of your website if you do not already have one.
  2. Use the following code to block specific hosts or IP addresses:
1
2
3
4
5
6
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} example.com [NC,OR]
    RewriteCond %{REMOTE_ADDR} 192.168.1.1
    RewriteRule .* - [F]
</IfModule>


Replace "example.com" with the hostname or domain name you want to block, and "192.168.1.1" with the IP address you want to block.

  1. Save the .htaccess file and upload it to your server.


Now, any requests coming from the specified hostnames or IP addresses will be blocked and the server will return a 403 Forbidden error to them.