多语言.htaccess重定向到子域

时间:2012-04-28 21:35:03

标签: php .htaccess redirect seo

我有一个PHP的多语言网站(西班牙语,英语,法语),主要的语言是英语。

如果$_SESSION['idm']设置为'en',则会加载包含翻译的文件。

我想这样设置:

如果用户语言是西班牙语

www.mydomain.commydomain.com - > es.mydomain.com

www.mydomain.com/video.php?id=3 - > es.mydomain.com/video.php?id=3

如果用户语言为法语

www.mydomain.com and mydomain.com - > fr.mydomain.com

www.mydomain.com/video.php?id=3 - > fr.mydomain.com/video.php?id=3

如果不是以上任何

www.mydomain.com and mydomain.com - > en.mydomain.com

www.mydomain.com/video.php?id=3 - > en.mydomain.com/video.php?id=3

我如何做到这一点,这是一个很好的SEO明智吗?

2 个答案:

答案 0 :(得分:3)

PHP中的

// check if the current domain is the generic one
$req_domain = $_SERVER['SERVER_NAME']; 
if ($req_domain == 'www.mydomain.com' || $req_domain == 'mydomain.com') {
  $subdomains = array ('fr' => 'fr.mydomain.com',
                       'es' => 'es.mydomain.com',
                       'en' => 'en.mydomain.com');


  // get the language from the session variable (if set), or use default
  $lang = 'en'; // default language
  if ( isset($_SESSION['idm']) && isset($subdomains[$_SESSION['idm']]) )
    $lang = $_SESSION['idm']; // selected language


  // redirect while maintaining the complete request URI
  header('Location: http://' . $domains[$lang] . $_SERVER['REQUEST_URI']);
  exit;
}

答案 1 :(得分:0)

对于搜索引擎优化而言,这比当前基于会话的方法更好,它隐藏了搜索引擎的语言变化。

稍微改动就是在主域上保留默认语言(en)。

使其发挥最佳效果:

确保页面链接是相对的,这样您就不会在每次点击时导致重定向。

将hreflang元数据添加到页面以指示翻译的位置。

不要强迫人们使用某种语言。确保它们可以轻松更改。

相关问题