許多網站會根據使用者的語言或地區,提供不同版本的網頁。hreflang
連結會告訴搜尋引擎網頁所有版本的網址,讓搜尋引擎顯示每種語言或區域的正確版本。
Lighthouse hreflang
稽核失敗的原因
Lighthouse 會標示錯誤的 hreflang
連結:
Lighthouse 會在網頁的 head
和回應標頭中檢查 hreflang
連結。
接著,Lighthouse 會檢查 hreflang
連結中是否有有效的語言代碼。Lighthouse 會回報任何含有無效語言代碼的 hreflang
連結。
Lighthouse 不會檢查區域代碼或網站地圖。
如何為網頁的每個版本定義 hreflang
連結
假設你有三個網頁版本:
- 英文版請見
https://example.com
https://es.example.com
的西班牙文版本- 德文版位於
https://de.example.com
有三種方法可以告訴搜尋引擎這些網頁是相同的。請選擇最適合您情況的方法。
方法 1:在每個網頁的 <head>
中加入 hreflang
連結:
<link rel="alternate" hreflang="en" href="https://example.com" />
<link rel="alternate" hreflang="es" href="https://es.example.com" />
<link rel="alternate" hreflang="de" href="https://de.example.com" />
每個網頁版本都必須連結至所有其他版本,包括本身。否則,搜尋引擎可能會忽略 hreflang
連結,或誤解這些連結。
如果網頁允許使用者選取語言,請使用 x-default
關鍵字:
<link rel="alternate" href="https://example.com" hreflang="x-default" />
選項 2:在 HTTP 回應中加入 Link
標頭:
Link: <https://example.com>; rel="alternate"; hreflang="en", <https://es.example.com>;
rel="alternate"; hreflang="es", <https://de.example.com>; rel="alternate"; hreflang="de"
選項 3:在網站地圖中新增語言版本資訊。
<url>
<loc>https://example.com</loc>
<xhtml:link rel="alternate" hreflang="es"
href="https://es.example.com"/>
<xhtml:link rel="alternate" hreflang="de"
href="https://es.example.com"/>
</url>
。hreflang
值的規範
hreflang
值一律必須指定語言代碼。- 語言代碼必須遵循 ISO 639-1 格式。
hreflang
值也可以包含選用的區域代碼。舉例來說,es-mx
是墨西哥西班牙文使用者,es-cl
則是智利西班牙文使用者。- 區域代碼必須採用 ISO 3166-1 alpha-2 格式。