Skip to content
返回发布与可发现性

Canonical 设置

配 canonical 标告搜索引擎哪个 URL 是权威版 — 防同页多 URL 可达时的重复内容罚。

为什么

当同内容多 URL 可达(http vs https、带/不带尾斜、带/不带查询参、www vs 非 www),Google 见重复内容要么任选一(可能选错)要么去索引全。canonical 标解此告 Google"此 URL 是权威版,索引此个忽略其他"。无 canonical,有 /launch 和 /launch/(尾斜)和 /launch?ref=ph 的站把排名信号分 3 路且排低于单一 canonical URL 的站。canonical 设置在发布尤关键因新页常多入口(博客链 /launch?ref=blog、Product Hunt 链 /launch?ref=ph)无 canonical 则每变体在索引中互竞。

何时使用

构建期间,作为 BaseLayout 或每页 head 一部分。发布前在 staging 爬验证、部署 24 小时内在生产验证。

如何做

1. 定 canonical URL 模式:https://ez4code.com/launch(https、无尾斜、无查询参、无 www)— 此是每页唯一真 URL
2. 在 BaseLayout 加 <link rel="canonical" href={canonical}>,canonical 从页路径建(非从 window.location,含查询参)
3. 每页 canonical 须自引:/launch canonical = https://ez4code.com/launch、/launch/[slug] canonical = https://ez4code.com/launch/[slug]
4. 分页:/launch?page=2 canonical 应指 /launch(首页)非 /launch?page=2 — 此把排名信号合到首页
5. i18n 页:/zh/launch canonical = https://ez4code.com/zh/launch(每语言版是自己 canonical,经 hreflang 连非 canonical)
6. 过滤/排序视图:/launch?subdomain=producthunt canonical = https://ez4code.com/launch(未过滤视图是 canonical;过滤视图不单独索引)
7. 为非 canonical URL 形设 301 重定向:http:// → https://、ez4code.com/launch/ → ez4code.com/launch(尾斜)、www.ez4code.com → ez4code.com
8. 爬验证:Screaming Frog 标任何 canonical 不匹配其 URL(重定向归一后)的页;发布前修所有标
9. Google Search Console 中发布后查"Duplicate without user-selected canonical"报告 — 此处任何页是你漏的 canonical 问题

示例

# Canonical 设置 — ez4code v2.0

## BaseLayout.astro(canonical 逻辑)
---
const canonical = `${SITE_URL}${prefix}${path}`;
---
<link rel="canonical" href={canonical}>

## 每页 canonical
/launch                    → https://ez4code.com/launch
/launch/pre-launch-checklist → https://ez4code.com/launch/pre-launch-checklist
/zh/launch                 → https://ez4code.com/zh/launch
/zh/launch/ph-preparation  → https://ez4code.com/zh/launch/ph-preparation

## 非 canonical 形(301 重定向到 canonical)
http://ez4code.com/launch    → 301 → https://ez4code.com/launch
ez4code.com/launch/         → 301 → https://ez4code.com/launch
www.ez4code.com/launch      → 301 → https://ez4code.com/launch
ez4code.com/launch?ref=ph   → canonical → https://ez4code.com/launch

## 分页
/launch?page=2  → canonical → https://ez4code.com/launch
/launch?page=3  → canonical → https://ez4code.com/launch

## 过滤
/launch?subdomain=producthunt → canonical → https://ez4code.com/launch

## 验证(Screaming Frog)
77 URL 爬
0"canonical 不匹配"标
0"无 canonical 重复"标
=> canonical 设置正确

## 发布后(Search Console)
"无用户选 canonical 的重复"报告:0 URL ✓

常见问题

  • ⚠从 window.location 而非页路径建 canonical — window.location 含查询参(?ref=ph),故每引荐变体得不同 canonical 且互竞;仅从路由路径建 canonical。
  • ⚠尾斜不一致 — /launch 和 /launch/ 对 Google 是不同 URL;无 301 重定向则两者都索引且分排名信号;选一形(无尾斜是惯例)并重定向另一。
  • ⚠canonical 到 404 或重定向 — 若 canonical URL 自身返 404 或 301,Google 忽略 canonical 并索引所找;验每 canonical URL 返 200。
  • ⚠i18n 页用 canonical 而非 hreflang — /zh/launch canonical 指 /launch 告 Google 去索引中文版;语言 alternate 用 hreflang 且每语言自引 canonical。

最佳实践

  • ✓从路由路径 + SITE_URL 常量建 canonical,永不从 window.location — 此确保查询参和片段永不入 canonical,跨引荐变体稳。
  • ✓在边缘(Cloudflare Pages _redirects 或 vercel.json)经 301 重定向强制单一 URL 形 — http→https、尾斜、www→非 www 应全为边缘重定向非每页逻辑。
  • ✓每页用自引 canonical 含 i18n 变体 — /zh/launch canonical 是 https://ez4code.com/zh/launch 非 https://ez4code.com/launch;hreflang 连变体、canonical 定每者权威。
  • ✓每次部署后跑 Screaming Frog"Canonical Mismatch"报告 — 此数小时非数月抓 canonical 回退(新页错 canonical)。