So I'm having a brain freeze with a pretty simple geo-redirection that I'm sure someone here will be able to solve for me.
I have ads from Facebook going to domain.com/folder/index.php?subid=whatever, where index.php is a simple php header redirect that passes the subid variable through to my affiliate url.
What I want to do is redirect everyone who is not from a specific country to index2.php. Am doing this as my affiliate link has geo-redirection, as most do, so doesn't play well with Facebook reviewers. Index2.php redirects to the offer directly so can be viewed from anywhere.
I'm hoping that a redirection by htaccess/GeoIP will allow the original link with the subids in it to remain untouched so they don't get lost during any redirection, hence easier to just redirect everyone else. I have asked my AM if I can get geo-redirection turned off on my links but haven't got a reply yet and I need to fix this asap, have a campaign running right now that I want to secure from periodic reviews.
According to MaxMind this can be used to block all but one country:
GeoIPEnable On GeoIPDBFile /path/to/GeoIP.dat SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry SetEnvIf GEOIP_COUNTRY_CODE MX AllowCountry # ... place more countries here Deny from all Allow from env=AllowCountry # Optional - use if you want to allow a specific IP address from the country you denied # (See http://httpd.apache.org/docs/1.3/mod/mod_access.html for more details) Allow from 10.1.2.3
# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ http://www.northamerica.com$1 [L]
Phewww, have managed to work around the issue using PHP redirecting people based on country/browser language. With respect to the above, as far as I understand that htaccess redirection is perhaps the fastest way to redirect, and given you can secure your htaccess file it's somewhat robust to inspection, but does it make it exceedingly difficult to pass variables through to your php files?
Hi Zeno, can you post the php code you used to do this? I am trying to do the same thing - redirect based on country.
Sure. Firstly, get mod_geoip working on your server - I just had my hosting support do it for me - http://www.maxmind.com/app/php.
Then I use the following PHP code for redirects:
[PHP]
<?php
$subid1 = $_GET['subid1'];
$country = getenv(GEOIP_COUNTRY_NAME);
$country_code = getenv(GEOIP_COUNTRY_CODE);
//simple redirect split based on country
switch($country_code) {
case 'US':
$redirect[0] = 'http://affurl.com/?a=XXXX&c=YYYY&s1='.".$subid1;
$redirect[1] = 'http://affurl2.com/?a=XXXX&c=YYYY&s1='.".$subid1;
$redirect[2] = 'http://affurl3.com/?a=XXXX&c=YYYY&s1='.".$subid1;
$number = mt_rand(0,2);
header("Location:$redirect[$number]");
exit;
default: // exceptions
header("Location: http://offerurl.com");
exit;
}
?>
[/PHP]