Sub topic 11
Implementing canonicals: link element, HTTP header, sitemaps
This chapter assumes you know the basic structure of an HTML page (head and body), what a query string is, and how HTTP status codes and response headers work. You will learn three ways to tell a search engine which URL is the canonical one, and how to keep them consistent.
A canonical URL is the preferred address for a page that can be reached at several addresses. Canonical signals are hints. Search engines usually follow them, but they can choose a different URL.
Method 1: the link element in the head
For HTML pages, the most common method is a link element with rel="canonical" inside the head.
<!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>Follow these rules:
- Use an absolute URL, with protocol and host. Relative paths are easy to get wrong.
- Use one canonical per page. If a page declares two different ones, search engines may ignore both.
- Put it in the
head, in the HTML the server returns. Search engines may not see a tag that is injected late by JavaScript. - Make the target a live page that returns 200, not a redirect or an error.
A parameterized variant points to the clean URL. This tells search engines which version should collect the signals:
<!-- 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">The clean page can also carry a canonical that points to itself. This is called a self-referencing canonical, and it protects the page from variants you did not anticipate.
Method 2: the HTTP header
A PDF has no head element, so it cannot hold a link tag. Send the canonical in a Link response header instead:
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, and rel="canonical" follows after a semicolon. You configure this in your web server or application code.
Method 3: sitemaps
A sitemap is an XML file listing the URLs you want indexed. Listing only canonical URLs in it is a weaker but useful signal. Include the clean URL and leave out parameterized variants, redirects, and pages marked noindex.
<?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>
</url>
</urlset>Generating canonicals in code
Hand-writing canonicals does not scale. A helper can build them. 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"Keep the signals consistent
The signals work together. If the link element says one URL and the sitemap lists another, the search engine has conflicting evidence and may pick for itself.
| Method | Best for | Limit |
|---|---|---|
| `link` element | HTML pages | Needs a `head` in the server response |
| `Link` header | PDFs and other non-HTML files | Needs server configuration |
| Sitemap | Site-wide reinforcement | Weaker hint on its own |
Pick one primary method per page type, and use the sitemap to back it up. Do not set both a header and a link element with different URLs for the same page.
Exercise
A PDF at https://www.example.com/guides/size-guide.pdf is also served at https://www.example.com/guides/size-guide. You prefer the second URL. How do you declare that, and why can't you use a link element?
Show answer
Send a Link response header on the PDF: Link: <https://www.example.com/guides/size-guide>; rel="canonical". A PDF has no head element, so it cannot contain a link element. Also list only the preferred URL in the sitemap so the signals agree.