许多网站会根据用户的语言或地区提供不同版本的网页。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:将 hreflang
链接添加到每个网页的 <head>
中:
<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 格式。