Installation #
To install Country Redirect, follow these steps:
- Download & unzip the file and place the
countryredirect
directory into yourcraft/plugins
directory - Install plugin in the Craft Control Panel under Settings > Plugins
- The plugin folder should be named
countryredirect
for Craft to see it.
Country Redirect works on Craft 2.4.x and higher, and requires PHP 7 or higher
Using the plugin #
Before you start using the plugin, you have to do 3 things:
- Download a updated country database database from MaxMind through the plugin settings
- Copy the configuration file
countryredirect.php
into the Craftconfig
directory, usuallycraft/config
- Modify the configuration file to match preferences, and make sure you setup the
countryMap
setting to match your locales.
Configuration #
<?php
return [
'*' => [
/*
* Enable for all users
*/
//'enabled' => true,
/*
* Or specifically logged in/anon users
*/
'enabled' => [
'loggedIn' => true,
'anonymous' => true,
],
/*
* Don't redirect bots/crawlers like GoogleBot, Bing etc.
*/
'ignoreBots' => true,
/*
* Add any special URL segments you want to ignore
* Eg. 'ignoreSegments' => [
* 'this/page/only/exists/in/one/locale',
* 'global-page'
* ],
*/
'ignoreSegments' => [ ],
/*
* Cookie name
*/
'cookieName' => 'countryRedirect',
/*
* The URL parameter that let a user manually select which locale they want to see
*/
'overrideLocaleParam' => 'selected-locale',
/*
* The URL parameter that indicates that a user was redirect
*/
'redirectedParam' => 'redirected',
/*
* Map a countrys two-letter ISO code to a Craft locale, and/or define a catch-all with a * asterix
* Here is a list of ISO country codes: http://www.nationsonline.org/oneworld/country_code_list.htm
* Example:
* 'countryMap' => [
* 'FR' => 'fr',
* 'DK' => 'da',
* // You can also send visitors to an arbitrary URL
* 'DE' => 'http://google.de',
* '*' => 'en_us',
* ]
*
* If you within a country have different regional languages, you can map the different languages to locales.
* Take Switzerland, with German, French, Italian and Romansh, as an example:
*
* 'countryMap' => [
* 'CH' => [
* 'fr' => 'fr',
* 'de' => 'de',
* ],
* '*' => 'en_us',
* ]
*
* This works by checking the Accept-Language header of the browser.
*
*/
'countryMap' => [ ],
/*
* If you want to show a banner that prompts visitors to their matching locale instead of redirecting them,
* you can define these here.
*
* The key here is the Craft locale id, not the country code. The variables {countryName} and {url} will
* be replaced.
*
* 'banners' => [
* 'en_us' => 'It looks like your visiting from {countryName}. Do you <a href="{url}">want to visited the international site?</a>'
* ],
*/
'banners' => [ ],
],
];
For a list of the ISO country codes, check out this overview
Navigation #
<h1>Select country</h1>
{% set titles = {
'en_us': 'Go to our US site',
'fr': 'Go to our French site',
'da': 'Go to our Danish site',
} %}
<nav class="nav">
{% for locale in craft.countryRedirect.getLinks() %}
{{locale.getLink({ title: titles[locale.getId()] })}}
{% endfor %}
</nav>
Visitor information #
Country Redirect has a few helper methods to access visitor information.
To access information:
{% set info = craft.countryRedirect.info() %}
{% if info %}
<p>Your located in {{ info.name }} ( {{ info.isoCode }})</p>
{% endif %}
To check if a user was redirected:
{% if craft.countryRedirect.redirected() %}
<p>You were redirected because of your location, which is {{ info ? info.isoCode }} ({{ info ? info.name }})</p>
{% endif %}
To display a message when a visitor overrides their locale:
{% if craft.countryRedirect.overridden() %}
<p>You have overridden your locale, which is now {{ craft.locale }}</p>
{% endif %}
Banner prompts #
If you want to show a banner that prompts visitors to their matching locale instead of redirecting them, you can define
the text for each locale with the banners
setting.
You can then access the text like this:
{% set banner = craft.countryRedirect.getBanner() %}
{% if banner %}
<div class="banner">
<p>{{ banner.getText() }}</p>
</div>
{% endif %}
Testing #
You can test it by using a free VPN account from TunnelBear
Database #
Geolocation data is provided by MaxMind. According to them, their databases is 99.8% accurate on a country level.
Accuracy #
Roadmap #
- Make it easier to map locales with a larger amount of country codes
- Port to Craft 3
- Make it possible to test locally
- Add environment checks
Reviews
The Country Redirect plugin has been great. It works exactly as described, was a snap to setup, and I love that you can update the geolocation database via a cron job. Fred’s support has been top notch too. If you’re looking to implement similar functionality I’d highly recommend checking this plugin out.
Frequently asked questions
- How many sites does 1 license cover?
It's pretty simple: 1 license covers 1 site / Craft installation.
- Does Google condone location based redirects?
After doing quite a bit of research including reading Google's own documentation on this, I've come to the following conclusions
- Location based redirect is fine: https://support.google.com/webmasters/answer/6144055?hl=en - but you should definitively read all the documentation of Google thoroughly to make sure its correctly setup in your case
- This is a bit old, but relevant: http://searchengineland.com/googles-matt-cutts-user-redirection-based-on-location-is-not-spam-174023
- Different locales should be annotated with rel=alternate hreflang annotations: https://support.google.com/webmasters/answer/189077
- Can I get country information without redirecting visitors?
Yep. Just set
enabled
to false.- Can I test the plugin locally?
Since you won't have a valid IP address when accessing the site from localhost, the plugin won't be able to detect the country.
- I'm having problems downloading the database
As a first step, check the logs (craft.log, phperrors.log and countryredirect.log) to see if it gives you any error messages.
- Do I have to update the database regularly?
Even if you don't do it regularly, the coverage will still be pretty good. If there is no good reason not to, I’d recommend that you do it once a month.
There is a special url you can call to update it automatically.
Releases
- Fixed: Make updateDatabase available by anonymous access
- Fixed: Fixed error when getCountryLocale would sometimes return array
- Fixed: Fixed bug that caused redirect to break in previous version
- Fixed: Fixed `enabled->loggedIn setting`. The setting will now be honored, and all users (anon and logged in) will be redirected by default.
- Fixed: Fixed check that skips redirect if in Live Preview mode
- Added: Added check to disable `redirected` url param if disabled
- Fixed: Fixed a bug where default config values was overwritten even if the config key wasn't in the config.php file
- Added: Added support for mapping regional languages of a country to locales
- Improved: Skip redirect check if in live preview mode
- Improved: Made banner check cookie name configurable
- Improved: Option to map countries to arbitrary URLs
- Added: Added support for banner prompts, so that you can display a banner instead of redirecting visitors automatically.
- Improved: Made resolving of database path more robust
- Added: Added check to make sure database path exists.
- Improved: Updated CrawlerDetect
- Improved: Simplified IP detection
- Added: Added ignoreSegments config setting, for ignoring certain URL global segments that only exists in one/all locales
- Added: Added template methods overridden(), redirected() and info()
- Improved: Improved the IP detection method accuracy, with specific detection for Ezoic, Sucuri, CloudFlare, Akamai, Fastly and standard proxies (HTTP_X_FORWARDED_FOR)
- Improved: Added ignoreBots setting, so Googlebot etc. won't be redirected.
- Fixed: The checksum routine now uses Guzzle instead of fopen
- Fixed: Now uses Guzzle/cURL to download the database, so servers with allow_url_open disabled won't throw up
- Improved: Removed PHP7-only features to make it compatible with PHP 5.4 and above.
- Improved: Added checks for console and action requests
- Added: Initial release
Currently available for Craft consulting and custom plugin development
Contact me