Sub topic 10
Canonical URLs and rel=canonical
This chapter assumes you know basic HTML structure (head and body), how URLs and query strings work, and what common HTTP status codes mean. It builds on the previous chapter, which showed how one page can be reachable at many URLs. Here you will learn what a canonical URL is, what rel=canonical tells a search engine, and what it does not.
What a canonical URL is
A canonical URL is the address a site owner names as the preferred version of a page. When several URLs return the same or nearly the same content, a search engine groups them into a cluster of duplicates. It then picks one URL from that cluster to index and show in results. That chosen URL is the canonical.
The rel=canonical annotation is how you suggest your preferred choice. It is a hint, not a command. The search engine weighs it against other signals and may pick a different URL.
Consolidating a cluster has practical benefits:
- Ranking signals, such as links pointing at any variant, are combined on one URL.
- Crawl effort is spent on one version instead of many.
- Reports and analytics show one URL per page instead of a split picture.
- Users see the clean URL in search results instead of one with tracking parameters.
Where the canonical fits in the pipeline
The search pipeline has three steps: crawl, index, and serve. Canonicalization happens between crawling and indexing. The crawler fetches a URL and renders it. The engine then compares the page with others it knows about. If it decides the page duplicates another, it keeps one URL as the canonical and treats the rest as alternates. Alternates are usually crawled less often and rarely appear in results.
The basic syntax
The most common form is a link element inside the head. This page declares itself as canonical, which is called a self-referencing canonical.
<!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, description, and canonical all describe the same page. That consistency matters, because a canonical that points to a page with different content is likely to be ignored.
A variant of the page points to 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">Both the parameterized URL and the clean URL return the same page. The tag tells the engine to consolidate signals on the clean one.
Rules for the href value
- Use an absolute URL with protocol and host. Relative paths work in some cases, but they are easy to resolve wrongly, especially with a
baseelement or when the page is served from several hosts. - Use exactly one canonical per page. If a page has several with different targets, the engine may ignore all of them.
- Place it in the
head. Alinkelement that appears in thebodyis not honored. - Point to a URL that returns 200 and can be indexed. Do not point to a redirect, a 404, or a page marked
noindex. - Match the form you actually serve: protocol,
wwwor not, trailing slash, and letter case. URL paths are case-sensitive, so/Shoesand/shoesare different URLs.
What counts as a duplicate
Canonical tags are for pages whose main content is the same or very close. Typical sources include:
| Variant type | Example | Usual canonical target |
|---|---|---|
| Tracking parameters | `?utm_source=newsletter` | URL without the parameter |
| Protocol | `http://` and `https://` | The `https://` URL |
| Host | `example.com` and `www.example.com` | The one you chose as primary |
| Trailing slash | `/shoes/` and `/shoes` | The one your site serves consistently |
| Sort or view parameters | `?sort=price` | Usually the unsorted URL |
| Print or AMP-style copies | `/shoes/print` | The main page |
| Syndicated copy | Your article republished elsewhere | The original, if the other site cooperates |
Not every parameter is a duplicate. A parameter such as ?color=red may show a different product image but identical text, which is a duplicate. A parameter such as ?page=2 shows different items, so it is a different page. Pointing page 2 at page 1 hides the items on page 2 from the index. Give each paginated page a self-referencing canonical.
Canonical for non-HTML files
A PDF has no head, so you declare its canonical in an HTTP response header.
HTTP/1.1 200 OK
Content-Type: application/pdf
Link: <https://www.example.com/guides/size-guide>; rel="canonical"The URL is in angle brackets, and rel="canonical" follows after a semicolon. The next chapter covers header and sitemap methods in more detail.
Building canonicals in code
Templates usually compute the canonical from the request URL. The helper below forces HTTPS, drops the fragment, and removes tracking parameters, while keeping parameters that change the 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"Two details are worth knowing. First, the loop copies the keys into an array before deleting, because deleting while iterating a live URLSearchParams can skip entries. Second, the helper keeps color, so ?color=red gets its own canonical. Whether that is right depends on your site. If color variants have identical text, you may prefer to drop color as well. This is a decision about your content, not something code can determine.
Why the engine may ignore your hint
Because rel=canonical is a hint, the engine compares it with other signals. A canonical is more likely to be followed when these agree with it:
- Redirects that lead to the canonical URL.
- The URL listed in your sitemap.
- Internal links that point to the canonical form.
- HTTPS being used instead of HTTP.
- The content of the two pages being truly the same.
If these signals disagree, the engine picks the URL it judges best. In a report you may see two statuses: the user-declared canonical (what you stated) and the Google-selected canonical (what the engine chose). When they differ, look for conflicting signals first.
Common reasons for an ignored canonical:
- The target page has noticeably different content.
- The target redirects, errors, or is blocked.
- Internal links and the sitemap point to a different variant.
- The tag is missing from the raw HTML and added by JavaScript that runs late, or JavaScript changes it to a different value.
- The page has more than one canonical tag, with conflicting values.
Checking a canonical
You can inspect the raw response to see what a crawler sees before any JavaScript runs.
curl -s "https://www.example.com/shoes/trail-running?color=red" | grep -i 'rel="canonical"'If nothing prints, the tag is either absent or added by script. For header-based canonicals, use curl -I and look for the Link header. Search engine consoles also have a URL inspection tool that reports both the declared and selected canonical.
Summary of the model
- Many URLs can serve one page, and the engine groups them into a cluster.
rel=canonicalsuggests which URL should represent the cluster.- It can be given as a
linkelement or as an HTTPLinkheader. - It works best when redirects, sitemaps, and internal links agree with it.
- The engine treats it as a hint and may choose another URL.
Exercise
A product page is available at https://www.example.com/boots?sort=price and at https://www.example.com/boots. The two pages show the same boots in a different order. Another URL, https://www.example.com/boots?page=2, shows the next set of boots. Which canonical should each URL declare, and why?
Show answer
The sorted URL (?sort=price) should declare https://www.example.com/boots, because it holds the same items and is a duplicate. The clean URL should declare itself (a self-referencing canonical). The ?page=2 URL should declare itself, not page 1, because it lists different boots. Pointing it at page 1 would tell the engine to drop those items from the index.
Exercise
You add <link rel="canonical" href="/boots"> to a page, and later find the search engine chose a different URL. Name two things to check.
Show answer
Any two of these: the href is relative, and should be absolute (https://www.example.com/boots); the target returns a redirect, error, or noindex instead of a 200 indexable page; the sitemap or internal links point to another variant; the content of the two pages differs; there is more than one canonical tag, or JavaScript changes the tag after load.