geoip将国家/地区重定向到403

时间:2013-05-05 02:50:58

标签: .htaccess mod-rewrite geoip

我遇到了使用geoip阻止国家/地区的问题。当我使用我的托管帐户实用程序来阻止国家/地区时,它会在.htaccess中创建以下脚本。问题是它似乎没有起作用(增加了美国但没有被阻止)。

GeoIPEnable on
RewriteEngine on
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CN$   
   RewriteRule ^.*$ - [F] 

因此,我将[OR]添加到列表中的最后一个国家/地区,但现在它会阻止所有内容,包括不在列表中的国家/地区。在这里我尝试删除美国但仍收到403消息。

GeoIPEnable on
RewriteEngine on
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CN$   [OR]
   RewriteRule ^.*$ - [F]

1 个答案:

答案 0 :(得分:1)

我是在不使用.htaccess的情况下完成的。我只是将以下代码片段放在header文件中。这可能对您有所帮助:

<?php 
include("includes/lcs/geoip.inc"); //Also load this file for its functions.
$serverIP=$_SERVER['REMOTE_ADDR']; //Get the IP address.
//echo $serverIP;
$gi = geoip_open("includes/lcs/GeoIP.dat",GEOIP_STANDARD); //Get the 2 letter country designation for the IP address.
$restricted_countries = array("US"); // list of restricted countries like array("NP", "US"); 
$country = geoip_country_code_by_addr($gi, $serverIP);


$restricted_pages = array("country_login","country_contact","country_password_forgotten","account"); 


if( (in_array($country,$restricted_countries)) and (!in_array($_GET['main_page'],$restricted_pages) ) )
{   
    header("location: https://www.yoursite.com/country_contact.html");

}
?>

这只是重定向到受限制国家/地区的country_contact.html

相关问题