Odyssee
Odyssee

Reputation: 2463

redirect domain according to extension and language

I'm trying to look for a solution for a multilanguage website. I have 4 domains:

According to the domain, the appropriate language has to be loaded.The language gets determined by ?lang=... I have tried modrewrite in .htaccess but it does not suit my needs. When a user visits domain.com/page the .htaccess does not process the rules. So I was thinking to do it with php. Currently, the language and the cookie gets set by this piece php:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en_eu';
}

switch ($lang) {
  case 'nl_be':
  $lang_file = 'nl_be.php';
  break;

  case 'nl_nl':
  $lang_file = 'nl_nl.php';
  break;

  case 'en_us':
  $lang_file = 'en_us.php';
  break;

  case 'en_uk':
  $lang_file = 'en_us.php';
  break;

  case 'en_eu':
  $lang_file = 'en_eu.php';
  break;

  default:
  $lang_file = 'en_eu.php';

}

include_once 'components/languages/'.$lang_file;
?>

I was thinking to add a some lines to determine the domain name and add some if statements to determine the language according to the domain. But I don't really know how and if this is the right way of doing it.

<?php
$url =  "//{$_SERVER['HTTP_HOST']}";

if ($url == domain.be) {
    $lang = 'nl_be';
}
elseif ($url == domain.uk) {
    $lang = 'en_uk';
}
else ( ) {
    $lang = 'en_us';
}
?>

Upvotes: 0

Views: 340

Answers (2)

Progrock
Progrock

Reputation: 7485

After reading this: https://support.google.com/webmasters/answer/182192?hl=en

Google suggest keeping different language versions on their own URLs.

As you already have the top level domains, I would just check the domain name and determine the language from that.

Then at the top of each page give the users language choice links:

English: example.com/node/1
French:  example.fr/node/1
etc.

Hide the current language link.

And supplement the above with link elements in the head:

<link rel="alternate" hreflang="es" href="http://example.es/node/1" />

Upvotes: 1

Odyssee
Odyssee

Reputation: 2463

I have found the answer in this thread How to default to another language based on domain

It makes use of SetEnvIf in .htaccess and php handles the rest

code in .htaccess:

SetEnvIf Host "\.be$" SITE_LANGUAGE=nl_be
SetEnvIf Host "\.uk$" SITE_LANGUAGE=en_uk

and then call the SITE_LANGUAGE with:

$_SERVER['SITE_LANGUAGE'];

code in PHP:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

$lang = $_SERVER['SITE_LANGUAGE'];

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en_eu';
}

switch ($lang) {
  case 'nl_be':
  $lang_file = 'nl_be.php';
  break;

  case 'nl_nl':
  $lang_file = 'nl_nl.php';
  break;

  case 'en_us':
  $lang_file = 'en_us.php';
  break;

  case 'en_uk':
  $lang_file = 'en_us.php';
  break;

  case 'en_eu':
  $lang_file = 'en_eu.php';
  break;

  default:
  $lang_file = 'en_eu.php';

}

include_once 'components/languages/'.$lang_file;
?>

Upvotes: 0

Related Questions