-
-
- Submit Updated Articles to Bing and Yandex with IndexNow in Joomla
- Add Published and Modified Date Meta Tags to Joomla
- Add Open Graph and Twitter Card Tags to Joomla
- Auto-Generate Meta Descriptions from Article Text in Joomla
- Add Noindex to Specific Pages in Joomla
- Verify Your Joomla Site with Google Search Console
-
- Add a View as Markdown Link to Joomla Articles
- Display the Last Modified Date in Joomla Articles
- Embed Iframes in Joomla
- Automatically Link Twitter (X) @mentions in Joomla Articles
- Open External Links in a New Tab in Joomla
- Display Estimated Reading Time in Joomla Articles
- Replace Old URLs in Joomla
- Search and Replace in Joomla
-
- Add a Footer Line to the Joomla Administrator
- Show a Staging Banner in the Joomla Administrator
- Remove the "All" Option from Admin List Limits in Joomla
- Add a Cookie Consent Banner to Joomla
- Make the Administrator Navigation Sticky in Joomla
- Add Google Fonts to Joomla
- Add a Facebook Messenger Chat Button to Joomla
- Add a Reading Progress Bar to Joomla
- Add a Custom Scrollbar to Joomla
- Show Confetti on a Thank You Page in Joomla
- Add Smooth Scrolling to Joomla
- Add a Back to Top Button in Joomla
- Add a WhatsApp Chat Button in Joomla
-
- Protect the Administrator with a Secret URL Key in Joomla
- Show a Welcome Message on First Login in Joomla
- Log Out Idle Administrators in Joomla
- Assign New Users to a Different User Group Automatically in Joomla
- Redirect Visitors by Country in Joomla
- Redirect Users After Login by User Group in Joomla
- Block User Registration with Certain Usernames in Joomla
- Block User Registration by Country in Joomla
- Block User Registration by Email Address in Joomla
- Block User Registration by IP Address in Joomla
- Remove the Forgot Password and Username Links in Joomla
- Remove the Generator Meta Tag in Joomla
- Let Users Log In with Their Email Address in Joomla
- Block Traffic by Country in Joomla
- Block Bot Traffic in Joomla
- Block Traffic by IP Address in Joomla
Redirect Visitors by Country in Joomla
Send visitors from each country to the right site, like your German store for Germany, while crawlers and returning visitors stay put, using one PHP snippet.
You run a separate site for Germany, or a page with prices in another currency, and visitors from that country keep landing on the wrong one. Asking them to pick a region costs a click that many never make. You can redirect visitors by country in Joomla with one PHP snippet in Code Snippets.
The snippet finds each visitor's country with TGeoIP, the free geolocation plugin that installs together with Code Snippets. The lookup runs on your own server from a local database, so visitor addresses never go to an outside service.
Before you start
TGeoIP needs the free MaxMind GeoLite2 database to tell where an address comes from. If you haven't set it up yet, enable geolocation first: add a free MaxMind license key to the System - Tassos GeoIP plugin and click Update Database.
The PHP snippet code
The snippet looks up the visitor's country on every frontend page load, before Joomla builds the page. When the country has a rule, the visitor gets a temporary (302) redirect to the address you set. Visitors from countries without a rule, and visitors whose country is unknown, stay on your site.
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Tassos\Framework\Helpers\Geo;
// Two-letter country code => where visitors from that country go.
$redirects = [
'DE' => 'https://example.de',
'FR' => 'https://example.fr',
];
// true: visitors land on the same page on the other site, so /about
// becomes https://example.de/about. false: they land on the address above.
$keepPath = false;
// true: redirect each visitor once and remember it for 30 days, so they
// can come back to this site on purpose. false: redirect every time.
$redirectOnce = true;
// true: let search engine crawlers through, so this site stays indexed.
$skipCrawlers = true;
// Do not edit below.
// No Tassos framework means no country lookup, so do nothing.
if (!class_exists(Geo::class))
{
return;
}
$app = Factory::getApplication();
$input = $app->getInput();
if ($redirectOnce && $input->cookie->getString('tcs_geo_redirected', '') !== '')
{
return;
}
$agent = $input->server->getString('HTTP_USER_AGENT', '');
if ($skipCrawlers && preg_match('/bot|crawl|spider|slurp|fetch/i', $agent))
{
return;
}
$redirects = array_change_key_case($redirects, CASE_UPPER);
$country = strtoupper((string) Geo::getVisitorCountryCode());
// Unknown country, or a country without a rule: stay here.
if ($country === '' || empty($redirects[$country]))
{
return;
}
$target = new Uri($redirects[$country]);
$current = Uri::getInstance();
// Never redirect to the site the visitor is already on.
if (strtolower($target->getHost()) === strtolower($current->getHost()))
{
return;
}
if ($keepPath)
{
$target->setPath(rtrim($target->getPath(), '/') . $current->getPath());
$target->setQuery($current->getQuery());
}
if ($redirectOnce)
{
$input->cookie->set('tcs_geo_redirected', '1', [
'expires' => time() + 30 * 86400,
'path' => '/',
'httponly' => true,
]);
}
$app->redirect($target->toString(), 302);
Change the 4 settings at the top:
$redirects: one line per country. The key is the 2-letter country code, for exampleDEfor Germany orFRfor France. You'll find every code in the ISO 3166-1 alpha-2 list. The value is the full address visitors go to.$keepPath: set it totruewhen the other site mirrors yours, so a visitor on/aboutlands on/aboutover there. Keepfalseto send everyone to the exact address in the list.$redirectOnce: withtrue, the snippet sets a cookie and redirects each visitor once in 30 days. Visitors who come back on purpose, for example through a "switch region" link, can browse your site freely. Withfalse, every page load redirects.$skipCrawlers: withtrue, Googlebot and other crawlers aren't redirected, so your pages stay in search results. Keep it on.
The snippet never redirects to the site the visitor is already on, so a rule that points to your own domain does nothing rather than loop. When the GeoIP database is missing or the address is unknown, nothing happens either.
How to add this snippet
- Install Code Snippets if it isn't installed already.
- Log into your Joomla backend.
- Go to Components → Tassos Code Snippets.
- Click New.
- Enter a title, for example Redirect by country.
- Set Code Type to PHP.
- Set Insertion Method to Auto-Insert Locations.
- Set Location to On Page Load.
- Paste the code above into the editor.
- Replace the sample rules with your own countries and addresses.
- Click Save & Close.
Redirect only from the home page
- Open the snippet and go to the Conditional Logic tab.
- Set Display on to Selected Pages.
- Click New Condition Set.
- Click Select Condition and choose Homepage.
- Click Save & Close.
Learn more in Conditional Logic.
Check that it works
- Add a rule for your own country that points to another site you own, and save the snippet.
- Open your site's home page in a private browser window.
You land on the other site. Go back and open your site again in the same window, and you stay, because the cookie from the first visit tells the snippet you've already been redirected. Your administrator isn't affected at any point, so you can always log in there and change the rules.
If visitors aren't redirected
- Your site sits behind Cloudflare or another proxy. Joomla may then see the proxy's address instead of the visitor's, so the snippet checks the wrong country. Go to System → Global Configuration, open the Server tab, set Behind Load Balancer to Yes, and click Save & Close. Leave it off when there's no proxy, because it lets visitors fake their address.
- The GeoIP database is missing. The snippet then does nothing. Open the System - Tassos GeoIP plugin and click Update Database.
- You already got redirected once. Clear your cookies or use a new private window.
To keep visitors from some countries out instead of sending them elsewhere, see Block Traffic by Country in Joomla. To learn more about how PHP snippets work, read the PHP snippet docs.