我有一个多语言网站(西班牙语,英语,法语),主要语言是英语。
如果$_SESSION['idm']
设置为"en",则会加载包含翻译的文件。
我想这样设置它:
如果用户语言是西班牙语
www.mydomain.com
和mydomain.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明智吗?
在 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;
}
对于SEO来说,它比当前基于会话的方法更好,该方法对搜索引擎隐藏了语言变体。
一个轻微的更改是将默认语言(en)保留在主域上。
要使其发挥最佳效果:
确保页面上的链接是相对的,这样您就不会在每次点击时导致重定向。
将 hreflang 元数据添加到页面以指示翻译的位置。
不要强迫人们学习一种语言。确保它们可以轻松更改。