• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

htaccess block visitors from a country's domain suffix

The contact form on our website has been repeatedly spammed by visitors from certain countries.  It also happens that we don't expect to do business with visitors from those countries.  Can I use .htaccess to block a country's domain suffix?  I understand that a large list of IP ranges can also be used, but I'd like to keep it simple.  For example, would
"deny from .us"  block visitors with the .us suffix?
0
ddantes
Asked:
ddantes
  • 6
  • 3
1 Solution
 
duncanb7Commented:
Could you conside to use this method as follows in which it will
seach for all United'IP and do block in .htaccess file. It can also
select other countries.
http://www.ip2location.com/blockvisitorsbycountry.aspx

Duncan
0
 
ddantesAuthor Commented:
Thanks for your comment.  That site generates a huge table of IP ranges which block a specific country.  I would prefer to keep the .htaccess code simple, and avoid using that method, if a simple ".us" would suffice.  Will that work?
0
 
duncanb7Commented:
Be reminded, when .us typing on client browser that wil be translated to number or IP by DNS server. so
you might need a tool or DNS tools to translate back to .us if you would like to keep a simple way
to solve the issue to block all .us address

Duncan
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
ddantesAuthor Commented:
Thank you.  Of course, I am using .us simply as an example, because I don't want to specify which country I plan to block. If I understood correctly,  htaccess won't block visitors from a specific domain suffix, such as .us, simply by adding code: "deny from .us"  and I must use a set of IP ranges.  Is that correct?   As an alternative to the "deny from" code, how about:

RewriteEngine On
RewriteCond %{HTTP_REFERER} \.us [NC]
RewriteRule .* - [F]
0
 
duncanb7Commented:
Proabably it will work. If not, try this

RewriteCond %{HTTP_REFERER} www\.#*$!\.us [NC,OR]
RewriteRule .* - [F]

There is a lot article in google if you search the word of "block subdomain .htaccess"

Duncan
0
 
duncanb7Commented:
if .htaccess approach is not fitting all of your need in future, you can
do it in php script that will let you more easier to program anything
or condition you want besides subdomain, Just redirect or forward all traffic to  php script file path for IP cross-check  or other check before user access your site.

Just for example php script reference only
<?php
echo  "<pre>REMOTE_ADRR = ".$_SERVER['REMOTE_ADDR']."</pre>";
if ($_SERVER['REMOTE_ADDR']) {
        $t =$_SERVER['REMOTE_ADDR'];
        $o = preg_replace('/(\\d+\\.\\d+\\.\\d+\\.\\d+)/e', 'get_host(\'\\1\')', $t);
        # $o = preg_replace('/(\\d+\\.\\d+\\.\\d+\\.\\d+)/e', 'gethostbyaddr(\'\\1\')', $t);
        echo '<pre>DOMAIN = '.$o.'</pre>';
		
		$str=".com";
		echo "<pre.Target to block subdomain = ".$str."<pre>";
		$pos=stripos($o,$str);
		if ($pos>0) 
		{
		echo "<pre>Found $str subdomain  at $t and it will block this IP address<pre>";
		exit();
		}

		
}

function get_host($ip){
        $ptr= implode(".",array_reverse(explode(".",$ip))).".in-addr.arpa";
        $host = dns_get_record($ptr,DNS_PTR);
        if ($host == null) return $ip;
        else return $host[0]['target'];
}

?>
<html>
<body>
<div style='color:blue'>No Domain is Block</div>
</body>
</html>

Open in new window

0
 
ddantesAuthor Commented:
Thank you for those details.  Before closing this question, I'm still unclear about a couple of points.  I am not seeking to block referrers or back links.  I'm wanting to block web traffic from visitors within a specified country domain -- in this example, let' say ".us".      So, returning to my original question, can I just use "deny from .us", or will that not work?
0
 
duncanb7Commented:
it may not work , otherwise why we will use RewriteRule/RewriteCond rule for a pattern search

Duncan
0
 
duncanb7Commented:
Thanks for your points

Have a nice day

Duncan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now