php.htaccesshttp-redirectgeolocationgeotargetting

MaxMind GEO_IP Redirect


been having this problem for a while! anyway i have downloaded Maxminds GEOIP php module and uploaded it to my server then I put this section of code into my index.php page

<?php
require_once("geoip/geoip.inc");
$gi = geoip_open("geoip/GeoIP.dat",GEOIP_STANDARD);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

$my_countriesrow = array('AD','AE','AF'......and so on);
$my_countrieseuro = array('AN','AT','BA','BE','BG','BY'.......and so on);
/* $my_country = array('GB','UK'); */

if (!in_array(strtolower($country), $my_countriesrow)){
    header('Location: https://www.website.com/row/index.php');
    exit();
}
elseif (!in_array(strtolower($country), $my_countrieseuro)){
    header('Location: https://www.website.com/euro/index.php');
    exit();
}
else {
    header('Location: https://www.website.com/index.php');
    exit();
}
?>

I have uploaded the page to my server and in Google Chrome i am getting.... This web page has a redirect loop

The web page at https://www.website.com/index.php has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

same as in firefox! Anyone got any Ideas as I am all out....

Thanks in advance!

-Phillip Dews

UPDATE

OK HAVE DECIDED TO GO DOWN THE .HTACCESS ROUTE! I have edited my htaccess file to look like this....

GeoIPEnable On
GeoIPDBFile /geoip/GeoIP.dat

# Europe
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AM$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AO$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AQ$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AR$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AS$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AW$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AX$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AZ$
RewriteRule ^(.*)$ https://www.Website.com/europe$1 [L]

# Rest Of World
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AD$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AE$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AF$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AG$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AI$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AL$
RewriteRule ^(.*)$ https://www.Website.com/row$1 [L]

But for some reason I am getting a 500 internal error, even after putting in every singe country it is still not working for me! really dont know what to do now as I have been working on this for over a week using different php scripts and now this. Hope anyone can help Thanks again in advance, the reason Im asking is that there a lot of country codes to put in by hand so dont want to put them all in only to find it will not work! many thanks again!

-Phillip


Solution

  • Well, if neither of your if statements matches, you're redirecting to the /index.php. Since the code you're editing serves index.php, the request to /index.php gets redirected ... again, to index.php. You probably want:

    $my_countrieseuro = array('AN','AT','BA','BE','BG','BY'.......and so on);
    
    if (in_array(strtolower($country), $my_countrieseuro)){
        // European version
        header('Location: https://www.website.com/euro/index.php');
        exit();
    } else {
        // International version
        header('Location: https://www.website.com/row/index.php');
        exit();
    }