currently I have an Zend application on default location
www.example.com/{controller}/{action}
but when a user visit from a specific country, how to detect their ip address and redirect them to this countryCode based url
www.example.com/uk/{controller}/{action}?
To detect the country from which the user are visiting I have written a helper:
require_once '../library/Ipinfo/ip2locationlite.class.php';
class Application_View_Helper_GetLocation extends Zend_View_Helper_Abstract
{
public function getLocation()
{
$ipLite = new ip2location_lite;
$ipLite->setKey('APIKEY');
//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();
$country = strtolower($locations['countryName']);
return "$country";
}
}
The above code will return the country name. If a user is visiting from France how can I rewrite the url so that the url becomes example.com/france/{controller}/{action}?
Refactor your view helper into a Controller Plugin and redirect.
A controller plugin can execute early in the request dispatch loop, therefore you can intercept a request and redirect to another response before any controller is loaded and rendered. Example below (warning, may contain bugs!)