---
title: "Redirect Visitors by Country in Joomla - Code Snippets"
description: "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."
url: "https://www.tassos.gr/docs/code-snippets/howto/access-control-security/redirect-by-country"
date: "2026-10-22T23:52:14+00:00"
language: "en-GB"
---

[ Home ](https://www.tassos.gr/docs) / [ Code Snippets ](https://www.tassos.gr/docs/code-snippets) / [ How-to Guides ](https://www.tassos.gr/docs/code-snippets/howto) / [ Access Control & Security ](https://www.tassos.gr/docs/code-snippets/howto/access-control-security)

#  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.

 ![Redirect Visitors by Country in Joomla](https://www.tassos.gr/images/2026/10/code-snippets-redirect-by-country-card.png) Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

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](#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](https://www.tassos.gr/docs/common/features/enable-geolocation-features-in-tassos-gr-extensions) first: add a free MaxMind license key to the **System - Tassos GeoIP** plugin and click **Update Database**.

## [The PHP snippet code](#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 example `DE` for Germany or `FR` for France. You'll find every code in the [ISO 3166-1 alpha-2 list](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). The value is the full address visitors go to.
- `$keepPath`: set it to `true` when the other site mirrors yours, so a visitor on `/about` lands on `/about` over there. Keep `false` to send everyone to the exact address in the list.
- `$redirectOnce`: with `true`, 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. With `false`, every page load redirects.
- `$skipCrawlers`: with `true`, 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.

 A redirect that runs on every page sends people away even when they followed a direct link to a specific page. If you only want to greet new visitors, limit the snippet to your home page with the free Homepage condition in Conditional Logic.

## [How to add this snippet](#how-to-add-this-snippet)

1. Install [Code Snippets](https://www.tassos.gr/docs/code-snippets/getting-started/how-to-install) if it isn't installed already.
2. Log into your Joomla backend.
3. Go to **Components** → **Tassos Code Snippets**.
4. Click **New**.
5. Enter a title, for example *Redirect by country*.
6. Set **Code Type** to **PHP**.
7. Set **Insertion Method** to **Auto-Insert Locations**.
8. Set **Location** to **On Page Load**.
9. Paste the code above into the editor.
10. Replace the sample rules with your own countries and addresses.
11. Click **Save & Close**.

## [Redirect only from the home page](#redirect-only-from-the-home-page)

1. Open the snippet and go to the **Conditional Logic** tab.
2. Set **Display on** to **Selected Pages**.
3. Click **New Condition Set**.
4. Click **Select Condition** and choose **Homepage**.
5. Click **Save & Close**.

Learn more in [Conditional Logic](https://www.tassos.gr/docs/code-snippets/functionality/conditional-logic).

## [Check that it works](#check-that-it-works)

1. Add a rule for your own country that points to another site you own, and save the snippet.
2. 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](#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](https://www.tassos.gr/docs/code-snippets/howto/access-control-security/block-countries). To learn more about how PHP snippets work, read the [PHP snippet docs](https://www.tassos.gr/docs/code-snippets/types/php).

 Last updated on Oct 15th 2026 18:10
