Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

htaccess block visitors from a country's domain suffix

Posted on 2014-07-14
9
Medium Priority
?
601 Views
Last Modified: 2014-07-14
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
Comment
Question by:ddantes
  • 6
  • 3
9 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40196152
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
 

Author Comment

by:ddantes
ID: 40196161
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
 
LVL 13

Expert Comment

by:duncanb7
ID: 40196233
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ddantes
ID: 40196247
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
 
LVL 13

Expert Comment

by:duncanb7
ID: 40196259
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
 
LVL 13

Expert Comment

by:duncanb7
ID: 40196266
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
 

Author Comment

by:ddantes
ID: 40196270
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
 
LVL 13

Accepted Solution

by:
duncanb7 earned 1000 total points
ID: 40196273
it may not work , otherwise why we will use RewriteRule/RewriteCond rule for a pattern search

Duncan
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40196279
Thanks for your points

Have a nice day

Duncan
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question