Probably not if it's a Perl module. Your hosting provider would need to have the Apache module for you to control it with .htaccess.
Printable View
Hi,
I have a question, how about redirect from different countries to different sites... let me show you how I'm trying to do:
<?PHP
include('geoip/geoip.inc'); //this file must exist in your directory
$gi = geoip_open('geoip/geoip.dat',GEOIP_STANDARD);
// get the ip of the visitor
$addr = getenv('REMOTE_ADDR');
// translate his ip to a country code
$country = geoip_country_code_by_addr($gi, $addr);
// close the geo database
geoip_close($gi);
$badcountry = array('PT');
// You can change the example with any countries that you want to block
// redirect them if they suck
if(in_array($country, $badcountry))
header('Location: http://www.domain.com'); //enter a url or page on your site
$averagecountry = array ('GR');
// redirect them if they suck
if(in_array($country, $averagecountry))
header('Location: http://www.domain1.com'); //enter a url or page on your site
$mediumcountry = array ('RO');
if(in_array($country, $mediumcountry))
header('Location: http://www.domain2.com'); //enter a url or page on your site
$highcountry = array ('SE');
if(in_array($country, $highcountry))
header('Location: http://www.domain3.com'); //enter a url or page on your site
?>
but I can't get it to work... I saw this code in here but I can't get it work...
The codes posted elsewhere on here only have a single redirect domain and they work, I use it myself. From what I can see of yours I think you need to have some else statements if using multiple if's but where I don't know, I'm not a programmer.
Hi Freedom, I posted some examples you can try here: How to redirect different countries to different sites
Ask them if they can install the GEO IP mod for Apache instead. :)
If they can't or dont want to, then I made script you can try out which works without .htaccess or the geoip.dat file if it's not available. I'll post it somewhere when I get a chance.
Instead of the geoip.dat file or .htaccess method, the script calls out to an external online GeoIP API lookup site for retrieving the country codes and then redirects or blocks visitors based on that information. Relying on an external site for redirecting/blocking people probably isn't a great idea, but it works.
There are a few more options like using a mysql database or a flat file database where you set all the country IP's into .php files seperated by IP ranges.
Thanks.
What is the best way? The "if else" or the "switch case select" ?
The switch is probably easier to work with and modify when you want to add more countries with another block like the example below.
switch ($country){
// start
case 'XX':
header('Location: http://www.domain1.com');
exit(0);
break;
// end
}
And just replace the 'XX' with the 2 letter country code you want to redirect.
Hi Mike, there's a few 100 pages, so really need to put the code in just one place, but affecting all the pages - is that possible please?
Yes, only if your server configuration permits, then you can prepend a .php file to all other .php files, with .htaccess or in php.ini.
EG: Automatically append or prepend files in a PHP script
I would push your host a little more though, if they are willing to install the perl mod, then maybe they can install the Apache GEOIP mod too.
Thanks Mike, will ask them and come back to you.