Sub topic 11
Implementing canonicals: link element, HTTP header, sitemaps
This chapter assumes you know basic HTML structure (head and body), how query strings work, and what common HTTP status codes mean. You should also know why duplicate URLs exist, which earlier chapters covered. Here the focus is on how to declare a canonical URL, three ways to do it, and the rules that make search engines trust the declaration.
What you are implementing
A canonical declaration is a hint. You tell search engines which URL in a set of duplicates you want indexed. Google treats it as a strong signal, not a command. It can pick a different URL if other signals disagree. You have three ways to send the hint:
| Method | Where it lives | Works for | Strength |
|---|---|---|---|
| `<link rel="canonical">` | Inside `<head>` of the HTML | HTML pages | Strong |
| `Link` HTTP header | HTTP response headers | HTML and non-HTML files such as PDFs | Strong |
| Sitemap listing | XML sitemap file | Any URL you list | Weaker |
Use one method per page when you can. Combining methods is allowed, but they must name the same URL.
The link element
Place one <link rel="canonical"> element in the <head>. The href is the URL you want indexed.
<!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.
A page can also canonicalize to itself. This is called a self-referencing canonical. It is good practice, because any parameterized copy of the page that is served with the same HTML then points back to the clean URL automatically.
Variants point to the clean URL
When a tracking or filter parameter creates a copy, the copy's HTML carries the canonical of the clean page.
<!-- 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">Search engines can crawl the variant, see the hint, and consolidate signals such as links onto the clean URL.
Rules for the link element
- Use an absolute URL: include the scheme and host (
https://www.example.com/...). Relative paths such as/shoes/trail-runningoften work, but they are easy to break when a page is served from another host or path. - Put it in the
<head>. Google ignores a canonical that appears in the<body>. A stray element earlier in the head can also cause problems: if markup before it closes the head early (for example, an<iframe>or<img>tag injected by a script), the parser may treat the rest as body content. - Declare one canonical per page. If two elements disagree, search engines may ignore both.
- The URL must return a 200 status. Pointing to a redirect, a 404, or a noindex page weakens or voids the hint.
- Match the protocol and host you actually serve. If the site runs on HTTPS, do not canonicalize to an
http://URL.
The Link HTTP header
A PDF, an image, or a plain text file has no <head>. For these, send the canonical in the response headers.
HTTP/1.1 200 OK
Content-Type: application/pdf
Link: <https://www.example.com/guides/size-guide>; rel="canonical"The URL goes inside angle brackets, followed by ; rel="canonical". This is useful when you publish the same document at several paths and want one indexed. It also works for HTML pages, which helps when you cannot edit templates but can change server configuration.
An nginx example that sets the header for one file:
location = /downloads/size-guide-print.pdf {
add_header Link '<https://www.example.com/guides/size-guide>; rel="canonical"';
}Check the result with curl -I https://www.example.com/downloads/size-guide-print.pdf. The Link line must appear in the output.
Sitemaps as a canonical signal
An XML sitemap lists URLs you want crawled. Google treats a URL's presence in a sitemap as a weak hint that you consider it canonical. Do not list parameterized variants, redirecting URLs, or noindex pages there.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/shoes/trail-running</loc>
<lastmod>2026-11-01</lastmod>
</url>
</urlset>A sitemap cannot replace the link element or header. It cannot say "this URL is a copy of that one". It only says "this URL is one I want indexed". If your sitemap lists ?color=red URLs while your pages canonicalize to the clean URL, you are sending conflicting signals. Generate sitemaps from the same logic that builds your canonical tags, so the two cannot drift apart.
How the signals combine
Search engines weigh several signals together: redirects, the canonical hint, sitemap inclusion, internal links, and HTTPS. This diagram shows the order of strength Google describes.
If your declared canonical conflicts with stronger or more numerous signals, the search engine may select another URL. For example, if every internal link points to the www version but your canonical names the bare domain, the choice is uncertain.
A helper that builds canonical URLs
Hand-writing canonicals in every template invites mistakes. A shared function keeps the rules in one place. This one forces HTTPS, drops the fragment, and removes tracking parameters, while keeping parameters that change the content (such as color).
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 matter. First, the loop copies the keys into an array before deleting, because deleting while iterating the live iterator can skip entries. Second, whether color=red belongs in the canonical depends on your decision about which variants deserve their own indexed page, which a later chapter covers.
Use the same function for the canonical tag, the sitemap <loc> values, and internal links. One source of truth prevents most conflicts.
Verifying your implementation
- View the rendered HTML, not only the source file. If JavaScript injects the canonical, Google can read it after rendering, but the signal is slower and less reliable. Prefer server-rendered tags.
- Run
curl -Ion non-HTML files to confirm theLinkheader. - Request a parameterized URL and confirm its canonical points to the clean URL.
- Request the canonical URL and confirm it returns 200 and carries a self-referencing canonical.
- In Google Search Console, open the URL Inspection tool. It shows both "User-declared canonical" and "Google-selected canonical". A mismatch means other signals outweigh your hint.
Quick reference of common errors
- A relative
hrefthat resolves to the wrong host. - Two canonical elements, one from the template and one from a plugin.
- A canonical in the body, or after markup that closes the head.
- A canonical that points to a redirecting or erroring URL.
- A sitemap that lists URLs your canonicals reject.
Exercise
A site serves its size guide as a PDF at /downloads/size-guide-print.pdf and also as an HTML page at /guides/size-guide. You want the HTML page indexed. Which canonical method do you use for the PDF, what does the declaration look like, and how do you verify it?
Show answer
A PDF has no head element, so use the Link HTTP header. Send Link: <https://www.example.com/guides/size-guide>; rel="canonical" in the PDF's response. Verify by running curl -I on the PDF URL and checking that the Link header appears with the absolute HTML page URL. Also confirm the HTML page returns 200 and has a self-referencing canonical.
Exercise
Your pages declare https://www.example.com/shoes/trail-running as canonical, but your sitemap lists https://www.example.com/shoes/trail-running?color=red. What problem does this create, and how do you fix it?
Show answer
The sitemap and the canonical tag send conflicting signals, so a search engine may choose a different canonical than the one you want. Fix it by generating sitemap URLs with the same function that builds canonical tags, so the sitemap lists only the clean URL.