【问题标题】:geoip redirect countries to 403geoip 将国家/地区重定向到 403
【发布时间】:2013-05-05 02:50:58
【问题描述】:

我在使用 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],但现在它阻止了所有内容,包括不在列表中的国家/地区。在这里我尝试删除 US 但仍然收到 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]

【问题讨论】:

  • 或者到列表的末尾表现得应有尽有。你是如何确认它没有工作的?
  • 美国不在列表中,但我在尝试访问该站点时收到 403 禁止消息。 (我在美国)。
  • 是否有尾随[OR]?
  • @PhpMyCoder 那就是列表末尾的 [OR]。当列表末尾没有 OR 时,它根本不会阻塞(添加美国进行测试)。当列表末尾带有 OR 时,它甚至会阻止不在列表中的国家(删除美国进行测试)。
  • 您确定您的 IP 地址显示为位于美国吗?您可能有一个 GeoIP 无法解析的 IP(尤其是如果您的网络主机使用的免费版本不准确)。尝试将以下规则添加到您的测试设置中:RewriteRule .* /test?code=%{ENV:GEOIP_COUNTRY_CODE} [R=temporary] 并查看当您的浏览器被重定向到此假路径时您在查询字符串中获得的代码值。如果您在地址栏中没有看到code=US,那么您的 IP 不会在您的 Web 主机上的 GeoIP 下解析为“美国”。

标签: .htaccess mod-rewrite geoip


【解决方案1】:

我没有使用.htaccess。我只是将以下 sn-p 代码放在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。

【讨论】:

  • 哇!非常感谢...有新版本吗?我不知道旧的 GeoIP.dat 数据库是否仍在更新,GeoIP2 是最新的 - 你有代码吗?
猜你喜欢
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多