Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess geoip将国家重定向到403_.htaccess_Mod Rewrite_Geoip - Fatal编程技术网

.htaccess geoip将国家重定向到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

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

因此,我将[或]添加到列表中的最后一个国家,但现在它阻止了所有内容,包括不在列表中的国家。在这里,我试图删除我们,但仍然收到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]

我没有使用
.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");

}
?>