doujing5726 2016-01-30 09:23 采纳率: 0%
浏览 54

根据扩展名和语言重定向域名

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

  • domain.com
  • domain.be
  • domain.nl
  • domain.uk

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';
}
?>
  • 写回答

2条回答 默认 最新

  • doufengsui7449 2016-01-30 09:45
    关注

    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;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类