【问题标题】:multi-language .htaccess redirect to subdomain多语言 .htaccess 重定向到子域
【发布时间】:2012-05-09 05:04:01
【问题描述】:

我有一个 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 是否明智?

【问题讨论】:

  • 我不认为使用 .htaccess 是可行的......你需要从 PHP 中完成。
  • @Aziz 那你会怎么用 PHP 做呢?

标签: php .htaccess redirect seo


【解决方案1】:

在 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;
}

【讨论】:

  • 谢谢,这非常有用!我希望我能对你投赞成票:)
  • 所以每个域都会有一整套代码?即每个子域将包含所有文件?
【解决方案2】:

对于 SEO 而言,它比您当前基于会话的方法更好,后者会向搜索引擎隐藏语言变化。

一个小改动是在主域上保留默认语言 (en)。

为了让它发挥最佳效果:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2012-04-21
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多