跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

HTML怎么做canonical标签_html canonical规范链接设置【必看】

canonical标签必须放在内且紧邻之后;href值须为绝对URL,与当前页实际地址严格一致,服务端生成、禁止JS注入,并通过GSC验证生效。 canonical标签该加在哪个位置 必须放在 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><head></code></div> 里面,且最好紧挨着 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><title></code></div> 或其他元信息之后。放错位置(比如塞进 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><body></code></div> )会导致搜索引擎完全忽略它——不是警告,是静默失效。 常见错误包括: 动态模板里拼接出错,生成了空 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">href</code></div> 值,如 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><link rel="canonical" href=""></code></div> 服务端渲染时未区分移动端/PC端,把 PC 页面的 canonical 错误塞给了 m.site.com 用了相对路径( <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">href="/article/123"</code></div> ),但站点同时支持 HTTP 和 HTTPS,结果混用协议导致归并失败 rel="canonical" 的 href 值怎么写才安全 必须是**绝对 URL**,协议、域名、路径全量写出,且与当前页面实际可访问地址严格一致(包括 trailing slash 是否存在、大小写、参数是否保留)。 例如当前页是 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">https://example.com/blog/post-1/</code></div> ,那么: 立即学习 “ 前端免费学习笔记(深入) ”; ✅ 正确: <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><link rel="canonical" href="https://example.com/blog/post-1/"></code></div> ❌ 错误: <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><link rel="canonical" href="/blog/post-1"></code></div> (缺协议+域名+末尾斜杠) ❌ 错误: <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><link rel="canonical" href="https://www.example.com/blog/post-1/"></code></div> (如果主域是 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">example.com</code></div> 而非 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">www.example.com</code></div> ,会触发跨域归并失败) 注意:不要试图用 canonical 解决分页、筛选、排序等动态参数问题。它只适合内容实质重复的页面(如带 UTM 参数的分享链接、HTTP/HTTPS 同页、www/non-www 镜像)。对参数页,优先用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">robots.txt</code></div> 或 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">noindex</code></div> 控制。 PHP/Node.js 模板中怎么动态生成 canonical 核心原则:别依赖客户端 JS 拼接,canonical 必须由服务端在 HTML 输出前写死。JS 注入的 canonical 不会被主流搜索引擎识别。 使用HTML,CSS,JavaScript开发Android应用程序 英文文字pdf版附源文件 如果你了解HTML,CSS和JavaScript,您已经拥有所需的工具开发Android应用程序。本动手本书展示了如何使用这些开源web标准设计和建造,可适应任何Android设备的应用程序 - 无需使用Java。您将学习如何创建一个在您选择的平台的Andr​​oid友好的网络应用程序,然后转换与自由PhoneGap框架到一个原生的Andr​​oid应用程序。了解为什么设备无关的移动应用是未来的潮流,并开始构建应用程序,提供更 下载 示例(PHP): <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto"><?php $canonical = 'https://' . $_SERVER['HTTP_HOST'] . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // 手动清理常见干扰参数,但不过度重写 $cleaned = preg_replace('/[?&](utm_[^&]*|ref=[^&]*)/', '', $canonical); ?> <link rel="canonical" href="<?= rtrim($cleaned, '/') . '/' ?>"></code></div> Node.js(Express)中类似: <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">res.locals.canonical = new URL(req.originalUrl, `https://${req.headers.host}`).href;</code></div> 关键点: 用 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">req.originalUrl</code></div> 而非 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">req.url</code></div> ,避免被中间件修改路径 手动处理重定向链中的 canonical(比如 /old-path → 301 → /new-path),旧页仍需指向新页,不能留空或指回自己 避免在 canonical 中保留 session_id、_ga 等纯追踪参数 验证 canonical 是否生效的实操方式 别只看源码有没有那行 tag。真实生效要看 Google 如何理解它: 用 Google Search Console 的「URL 检查」工具,输入目标页,查看「页面索引状态」下的「规范网址」字段是否为你设置的值 搜索 <div class="cxy-code-block"><button class="cxy-code-copy" aria-label="复制代码">复制</button><code class="language-auto">site:example.com "关键词"</code></div> ,对比搜索结果中展示的 URL 和你设的 canonical 是否一致 检查服务器日志:如果大量 canonical 指向一个 URL,但该 URL 返回 404 或 500,Google 会退回使用原始 URL,且不报错 最容易被忽略的是缓存问题:CDN 或反向代理可能缓存了旧版 HTML(含过期 canonical),刷新源站后必须同步 purge 缓存,否则搜索引擎抓到的仍是错的。 </div> <!-- 文章导航 --> <nav class="cxy-article-nav" aria-label="文章导航"> <a href="https://www.vqhf.com/article/19157.html" class="cxy-article-nav-link cxy-article-nav-prev"> <span>‹</span> <div> <div class="cxy-article-nav-label">上一篇</div> <div class="cxy-article-nav-title">jquery选择器介绍</div> </div> </a> <a href="https://www.vqhf.com/article/19173.html" class="cxy-article-nav-link cxy-article-nav-next"> <div> <div class="cxy-article-nav-label">下一篇</div> <div class="cxy-article-nav-title">html5各种页面切换效果和模态对话框用法总结_html5教程技巧</div> </div> <span>›</span> </a> </nav> </div> </article> <!-- 相关文章列表 --> <section class="cxy-content-section" style="margin-top: 20px;"> <h2 class="cxy-widget-title" style="margin-bottom: 20px;">相关文章</h2> <ul class="cxy-article-list"> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19157.html" class="cxy-article-link">jquery选择器介绍</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19152.html" class="cxy-article-link">split()方法怎么用</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19147.html" class="cxy-article-link">JavaScript如何实现打印功能_window.print()如何定制?</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19124.html" class="cxy-article-link">PS简单制作3D立体文字</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19084.html" class="cxy-article-link">JavaScript DOM操作:精确替换HTML元素内文本中的特定部分</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19051.html" class="cxy-article-link">如何使用 JavaScript 动态过滤 HTML 表格中的区域数据</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/18984.html" class="cxy-article-link">动态生成表格:遍历 JSON 数组构建行与单元格</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/18977.html" class="cxy-article-link">vue搭建博客难吗</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19173.html" class="cxy-article-link">html5各种页面切换效果和模态对话框用法总结_html5教程技巧</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19174.html" class="cxy-article-link">深入理解 Express.js 中间件的 next() 参数</a> <div class="cxy-article-meta">2026-10-06</div> </li> </ul> </section> </div> <!-- 右侧边栏 --> <aside class="cxy-sidebar cxy-sidebar-right" aria-label="侧边栏"> <!-- 推荐阅读 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">推荐阅读</h3> <div class="cxy-recommend-grid"> <a href="https://www.vqhf.com/article/19174.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="深入理解 Express.js 中间件的 next() 参数"> </div> <div class="cxy-recommend-title">深入理解 Express.js 中间件的 next() 参数</div> </a> <a href="https://www.vqhf.com/article/19173.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="html5各种页面切换效果和模态对话框用法总结_html5教程技巧"> </div> <div class="cxy-recommend-title">html5各种页面切换效果和模态对话框用法总结_html5教程技巧</div> </a> <a href="https://www.vqhf.com/article/19171.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="HTML怎么做canonical标签_html canonical规范链接设置【必看】"> </div> <div class="cxy-recommend-title">HTML怎么做canonical标签_html canonical规范链接设置【必看】</div> </a> <a href="https://www.vqhf.com/article/19157.html" class="cxy-recommend-item"> <div class="cxy-recommend-thumb"> <img src="https://www.vqhf.com/static/v0/images/article-6.jpg" alt="jquery选择器介绍"> </div> <div class="cxy-recommend-title">jquery选择器介绍</div> </a> </div> </div> <!-- 最新文章 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">最新文章</h3> <ul class="cxy-article-list"> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19174.html" class="cxy-article-link">深入理解 Express.js 中间件的 next() 参数</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19173.html" class="cxy-article-link">html5各种页面切换效果和模态对话框用法总结_html5教程技巧</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19171.html" class="cxy-article-link">HTML怎么做canonical标签_html canonical规范链接设置【必看】</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19157.html" class="cxy-article-link">jquery选择器介绍</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19152.html" class="cxy-article-link">split()方法怎么用</a> <div class="cxy-article-meta">2026-10-06</div> </li> <li class="cxy-article-item"> <a href="https://www.vqhf.com/article/19147.html" class="cxy-article-link">JavaScript如何实现打印功能_window.print()如何定制?</a> <div class="cxy-article-meta">2026-10-06</div> </li> </ul> </div> <!-- 热门标签 --> <div class="cxy-widget"> <h3 class="cxy-widget-title">热门标签</h3> <div class="cxy-article-tag-list"> <a href="https://www.vqhf.com/tag/961" class="cxy-article-tag-item">html</a> </div> </div> </aside> </div> </div> </main> <!-- 页脚 --> <footer class="cxy-footer"> <div class="cxy-container"> <div class="cxy-footer-links" style="justify-content: center;"> <a href="https://www.vqhf.com/22.html" class="cxy-footer-link">免责声明</a> <a href="https://www.vqhf.com/23.html" class="cxy-footer-link">关于我们</a> <a href="https://www.vqhf.com/24.html" class="cxy-footer-link">联系我们</a> <a href="https://www.vqhf.com/25.html" class="cxy-footer-link">投诉删除</a> <a href="https://www.vqhf.com/26.html" class="cxy-footer-link">投稿须知</a> </div> <div class="cxy-footer-copyright" style="text-align: center; margin-top: 20px;"> Copyright 2026 趣航编程网 版权所有 <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank" style="color: inherit;">黔ICP备2025063733号</a> </div> <div style="text-align: center; margin-top: 10px; font-size: 12px; color: #999;">免责声明:本站信息来源于网络,仅供学习使用,版权归原作者所有,如有不愿意被转载的情况,请联系QQ:182111886,通知我们删除已转载的信息。</div> </div> </footer> <!-- 返回顶部按钮 --> <button class="cxy-back-to-top" id="backToTop" aria-label="返回顶部">⇧</button> <!-- 公共脚本 --> <script src="https://www.vqhf.com/static/v0/js/common.js"></script> <div class="hide"> </div> </body> <script> (function() { var al = document.createElement("script"); al.src = "/api/log?action=views&code=200&id=19171&path=http%3A%2F%2Fwww.vqhf.com%2Farticle%2F19171.html&type=archive&nonce="+Date.now(); document.body.appendChild(al); })(); </script> </html>