.htaccess geoip将国家重定向到403
我在使用geoip阻止国家时遇到问题。当我使用我的托管帐户实用程序阻止国家时,它会在.htaccess中创建以下脚本。问题是它似乎不起作用(添加了我们,但没有被阻止) 因此,我将[或]添加到列表中的最后一个国家,但现在它阻止了所有内容,包括不在列表中的国家。在这里,我试图删除我们,但仍然收到403信息.htaccess geoip将国家重定向到403,.htaccess,mod-rewrite,geoip,.htaccess,Mod Rewrite,Geoip,我在使用geoip阻止国家时遇到问题。当我使用我的托管帐户实用程序阻止国家时,它会在.htaccess中创建以下脚本。问题是它似乎不起作用(添加了我们,但没有被阻止) 因此,我将[或]添加到列表中的最后一个国家,但现在它阻止了所有内容,包括不在列表中的国家。在这里,我试图删除我们,但仍然收到403信息 GeoIPEnable on RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$ [OR] RewriteCon
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]
我没有使用
.htaccess
。我刚刚将下面的代码片段放在头文件中。这可能对您有所帮助:
<?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\u contact.html
。或列表末尾。你是如何确认它不起作用的?美国不在名单中,但我在尝试访问该网站时收到403禁止信息。(我在美国)。是否有尾随的[或]
?@PhpMyCoder,在列表末尾有[或]。当没有或在列表末尾时,它根本不会阻塞(将我们添加到测试中)。当使用或位于列表末尾时,它甚至会阻止不在列表中的国家(删除我们进行测试)。您确定您的IP地址显示为基于美国吗?您可能有一个GeoIP无法解析的IP(特别是如果您的web主机使用的是不准确的免费版本)。尝试将以下规则添加到测试设置中:RewriteRule.*/test?code=%{ENV:GEOIP\u COUNTRY\u code}[R=temporary]
并查看当浏览器重定向到此伪路径时,查询字符串中的代码值。如果您在地址栏中看不到code=US
,则您的IP不会解析为web主机上GeoIP下的“US”。哇!非常感谢。。。有新版本吗?我不知道旧的GeoIP.dat数据库是否仍在更新,GeoIP2是最新的-您有这方面的代码吗?
<?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");
}
?>