您在這裡

多語言網站之間用rel="alternate" hreflang="x"鍊接起來

James Qi 在 2013年8月15日 - 16:06 發表

  我們用Drupal做過桌面版網站、手機版網站,中文版網站、英文版網站、更多語言版本網站,這些站之間除了頁面看得見的互相鍊接以外,還可以而且應該在源代碼中進行鍊接,以便讓搜索引擎更了解頁面之間的關系。

  請看Google的推薦:

  手機版鍊接我們以前添加過,現在也來添加多語言版本,Drupal 7中可以修改html.tpl.php來實現,Drupal 6可以通過修改page.tpl.php來實現。

  Drupal 7的代碼例子如下:

<?php

$server_name=$_SERVER['SERVER_NAME'];
$request_uri=$_SERVER['REQUEST_URI'];

$array_lang=array("af","sq","ar","hy","az","bg","ca","zh-hans","zh-hant","hr","cs","da","nl","eo","et","fi","fil","fr","gl","ka","de","el","hu","is","ga","it","ja","ko","lv","lt","mk","ms","mt","fa","pl","pt","ro","ru","sr","sk","sl","es","sw","sv","th","tr","uk","vi","cy");
global $base_url;
$url="http://$server_name$request_uri";
$url_path=str_replace("$base_url/",'',$url);
$lang_code=strtok($url_path,'/');
if (in_array($lang_code,$array_lang)) {
  $url_default=$base_url.substr($url_path,strlen($lang_code));
} else {
  $lang_code='en';
  $url_default=$url;
}
//print "  <link rel=\"alternate\" hreflang=\"x-default\" href=\"$url_default\" />\n";
print "  <link rel=\"alternate\" hreflang=\"en\" href=\"$url_default\" />\n";

foreach ($array_lang as $lang_code1) {
  $url_lang=str_replace($base_url,"$base_url/$lang_code1",$url_default);
  print "  <link rel=\"alternate\" hreflang=\"$lang_code1\" href=\"$url_lang\" />\n";
}

?>

  多語言的手機版和多語言的電腦版中都可以在<head></head>之間加入以上部分。

  查看網頁源代碼,效果類似如下:

<link rel="alternate" media="handheld" href="http://m.industry.bizdirlib.com/sic01/uk/node/2"/>
  <link rel="canonical" href="http://industry.bizdirlib.com/sic01/uk/node/2" />
  <link rel="alternate" hreflang="x-default" href="http://m.industry.bizdirlib.com/sic01/node/2" />
  <link rel="alternate" hreflang="en" href="http://m.industry.bizdirlib.com/sic01/node/2" />
  <link rel="alternate" hreflang="af" href="http://m.industry.bizdirlib.com/sic01/af/node/2" />
  <link rel="alternate" hreflang="sq" href="http://m.industry.bizdirlib.com/sic01/sq/node/2" />
  <link rel="alternate" hreflang="ar" href="http://m.industry.bizdirlib.com/sic01/ar/node/2" />
  <link rel="alternate" hreflang="hy" href="http://m.industry.bizdirlib.com/sic01/hy/node/2" />
  <link rel="alternate" hreflang="az" href="http://m.industry.bizdirlib.com/sic01/az/node/2" />
  <link rel="alternate" hreflang="bg" href="http://m.industry.bizdirlib.com/sic01/bg/node/2" />
  <link rel="alternate" hreflang="ca" href="http://m.industry.bizdirlib.com/sic01/ca/node/2" />
  <link rel="alternate" hreflang="zh-hans" href="http://m.industry.bizdirlib.com/sic01/zh-hans/node/2" />
  <link rel="alternate" hreflang="zh-hant" href="http://m.industry.bizdirlib.com/sic01/zh-hant/node/2" />
  <link rel="alternate" hreflang="hr" href="http://m.industry.bizdirlib.com/sic01/hr/node/2" />
  <link rel="alternate" hreflang="cs" href="http://m.industry.bizdirlib.com/sic01/cs/node/2" />
  <link rel="alternate" hreflang="da" href="http://m.industry.bizdirlib.com/sic01/da/node/2" />
  <link rel="alternate" hreflang="nl" href="http://m.industry.bizdirlib.com/sic01/nl/node/2" />
  <link rel="alternate" hreflang="eo" href="http://m.industry.bizdirlib.com/sic01/eo/node/2" />
  <link rel="alternate" hreflang="et" href="http://m.industry.bizdirlib.com/sic01/et/node/2" />
  <link rel="alternate" hreflang="fi" href="http://m.industry.bizdirlib.com/sic01/fi/node/2" />
  <link rel="alternate" hreflang="fil" href="http://m.industry.bizdirlib.com/sic01/fil/node/2" />
  <link rel="alternate" hreflang="fr" href="http://m.industry.bizdirlib.com/sic01/fr/node/2" />
  <link rel="alternate" hreflang="gl" href="http://m.industry.bizdirlib.com/sic01/gl/node/2" />
  <link rel="alternate" hreflang="ka" href="http://m.industry.bizdirlib.com/sic01/ka/node/2" />
  <link rel="alternate" hreflang="de" href="http://m.industry.bizdirlib.com/sic01/de/node/2" />
  <link rel="alternate" hreflang="el" href="http://m.industry.bizdirlib.com/sic01/el/node/2" />
  <link rel="alternate" hreflang="hu" href="http://m.industry.bizdirlib.com/sic01/hu/node/2" />
  <link rel="alternate" hreflang="is" href="http://m.industry.bizdirlib.com/sic01/is/node/2" />
  <link rel="alternate" hreflang="ga" href="http://m.industry.bizdirlib.com/sic01/ga/node/2" />
  <link rel="alternate" hreflang="it" href="http://m.industry.bizdirlib.com/sic01/it/node/2" />
  <link rel="alternate" hreflang="ja" href="http://m.industry.bizdirlib.com/sic01/ja/node/2" />
  <link rel="alternate" hreflang="ko" href="http://m.industry.bizdirlib.com/sic01/ko/node/2" />
  <link rel="alternate" hreflang="lv" href="http://m.industry.bizdirlib.com/sic01/lv/node/2" />
  <link rel="alternate" hreflang="lt" href="http://m.industry.bizdirlib.com/sic01/lt/node/2" />
  <link rel="alternate" hreflang="mk" href="http://m.industry.bizdirlib.com/sic01/mk/node/2" />
  <link rel="alternate" hreflang="ms" href="http://m.industry.bizdirlib.com/sic01/ms/node/2" />
  <link rel="alternate" hreflang="mt" href="http://m.industry.bizdirlib.com/sic01/mt/node/2" />
  <link rel="alternate" hreflang="fa" href="http://m.industry.bizdirlib.com/sic01/fa/node/2" />
  <link rel="alternate" hreflang="pl" href="http://m.industry.bizdirlib.com/sic01/pl/node/2" />
  <link rel="alternate" hreflang="pt" href="http://m.industry.bizdirlib.com/sic01/pt/node/2" />
  <link rel="alternate" hreflang="ro" href="http://m.industry.bizdirlib.com/sic01/ro/node/2" />
  <link rel="alternate" hreflang="ru" href="http://m.industry.bizdirlib.com/sic01/ru/node/2" />
  <link rel="alternate" hreflang="sr" href="http://m.industry.bizdirlib.com/sic01/sr/node/2" />
  <link rel="alternate" hreflang="sk" href="http://m.industry.bizdirlib.com/sic01/sk/node/2" />
  <link rel="alternate" hreflang="sl" href="http://m.industry.bizdirlib.com/sic01/sl/node/2" />
  <link rel="alternate" hreflang="es" href="http://m.industry.bizdirlib.com/sic01/es/node/2" />
  <link rel="alternate" hreflang="sw" href="http://m.industry.bizdirlib.com/sic01/sw/node/2" />
  <link rel="alternate" hreflang="sv" href="http://m.industry.bizdirlib.com/sic01/sv/node/2" />
  <link rel="alternate" hreflang="th" href="http://m.industry.bizdirlib.com/sic01/th/node/2" />
  <link rel="alternate" hreflang="tr" href="http://m.industry.bizdirlib.com/sic01/tr/node/2" />
  <link rel="alternate" hreflang="uk" href="http://m.industry.bizdirlib.com/sic01/uk/node/2" />
  <link rel="alternate" hreflang="vi" href="http://m.industry.bizdirlib.com/sic01/vi/node/2" />
  <link rel="alternate" hreflang="cy" href="http://m.industry.bizdirlib.com/sic01/cy/node/2" />

 

回應

最近收到Google Webmaster Tools的提醒郵件,接到下面Incorrect rel-alternate-hreflang implementation這樣的通知:

Dear webmaster of http://sic08.bizdirlib.com/,

We have noticed that on a number of the pages on your site the rel-alternate-hreflang implementation is incorrect. For example:

http://sic08.bizdirlib.com/m/node/10088

In particular, there seems to be a problem with missing or incorrect bi-directional linking (when page A links with hreflang to page B, there must be a link back from B to A as well). When our algorithms detect conflicting signals like on these pages, they may make indexing and ranking decisions that do not match what you intended.

To learn more about rel-alternate-hreflang, see the following Help Center article: https://support.google.com/webmasters/answer/189077

If you have any questions about how to resolve this issue, please visit our Help Forum:

http://productforums.google.com/forum/#!categories/webmasters/internationalization

Sincerely,
The Google Search Quality Team
詳細查看相關資料後,覺得是x-default的用法可能有誤,這個用于多語言的選擇頁面,我們可以在模闆中去掉這行。

James Qi / 祁勁松

您好!我上面這篇寫的是多語言網站加對應頁面的鍊接,您的日文網站如果是純日文的就沒法添加也無需添加了,如果頁面有對應的中文或者英文等版本網站的頁面才需要添加這種鍊接。

舉例,如果您的日文網站頁面http://ja.example.com/page.html有對應的中文頁面http://zh.example.com/page.html,就在這個日文網站的源代碼中添加一行:

<link rel="alternate" hreflang="zh" href="http://zh.example.com/page.html" />

反過來在中文頁面中添加一行指向日文網站對應頁面的鍊接:

<link rel="alternate" hreflang="ja" href="http://ja.example.com/page.html" />

James Qi / 祁勁松

發表新回應

Plain text

  • 不允許使用 HTML 標籤。
  • 自動將網址與電子郵件地址轉變為連結。
  • 自動斷行和分段。