Sub topic 10
Canonical URLs and rel=canonical
This chapter assumes you know basic HTML structure, URLs with query strings, and HTTP status codes. It builds on the previous chapter about URL variants and duplicate content.
What a canonical URL is
A canonical URL is the version of a page that you want search engines to treat as the main one. When several URLs return the same or nearly the same content, a search engine groups them into a cluster. It then picks one URL from the cluster to index and show in results. The rel=canonical annotation is how you tell the search engine which URL you prefer.
The annotation is a hint, not a command. Search engines weigh it against other signals, such as redirects, internal links, and sitemaps. They can choose a different URL if the signals disagree.
The link element
Place one link element with rel="canonical" in the head. Use an absolute URL, with the protocol and host included.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Trail Running Shoes for Beginners | Example Store</title>
<meta name="description" content="Compare cushioned trail running shoes for first-time runners, with fit tips and a size guide.">
<link rel="canonical" href="https://www.example.com/shoes/trail-running">
</head>
<body>
<h1>Trail Running Shoes for Beginners</h1>
</body>
</html>The title, the description, and the canonical all describe the same page. A relative URL such as /shoes/trail-running can be misread, so avoid it.
Pointing variants at the clean URL
Parameters often create duplicates. A tracking parameter does not change the content, so the variant should name the clean URL.
<!-- Served at https://www.example.com/shoes/trail-running?color=red&utm_source=newsletter -->
<link rel="canonical" href="https://www.example.com/shoes/trail-running">Here the search engine learns that the ?color=red&utm_source=newsletter version should be consolidated onto the clean URL. This only makes sense if the content is the same. If color=red shows a genuinely different set of products, think carefully before pointing it elsewhere.
Self-referencing canonicals
The canonical page should also name itself. A self-referencing canonical protects you when a parameterized variant appears that you did not plan for, because the copied tag still points to the clean URL.
The HTTP header
A PDF has no head element. For non-HTML files, send the canonical in a Link response header.
HTTP/1.1 200 OK
Content-Type: application/pdf
Link: <https://www.example.com/guides/size-guide>; rel="canonical"Generating the URL in code
If your templates build the canonical, write one helper and reuse it. This one forces HTTPS, drops the fragment, and removes tracking parameters while keeping parameters that change content.
const TRACKING_PARAMS = new Set(["gclid", "fbclid"]);
function canonicalUrl(rawUrl) {
const url = new URL(rawUrl);
url.protocol = "https:";
url.hash = "";
for (const key of [...url.searchParams.keys()]) {
if (key.startsWith("utm_") || TRACKING_PARAMS.has(key)) {
url.searchParams.delete(key);
}
}
return url.toString();
}
// canonicalUrl("http://www.example.com/shoes?utm_source=news&color=red#reviews")
// returns "https://www.example.com/shoes?color=red"Common errors
- Several
rel=canonicaltags on one page with different values. The search engine may ignore all of them. - A canonical placed in the
body, where it is not recognized. - A relative URL or a URL with the wrong host or protocol.
- A canonical that points to a page returning an error or a redirect.
Exercise
A page at https://www.example.com/shoes?sort=price&utm_medium=email shows the same products as https://www.example.com/shoes, only reordered. What canonical should it declare, and where should the tag go?
Show answer
It should declare <link rel="canonical" href="https://www.example.com/shoes"> in the head. The sort order and tracking parameter do not change which content the page is about, so the variant should point to the clean absolute URL.