Canonical and alternate links
A canonical link names the preferred URL when the same content is reachable at several addresses; alternate links point to translations and to the same page in other formats, such as JSON or Markdown. Both can be written in the HTML head or sent as HTTP Link headers.
Type: IntermediateOn this page
One documentation page rarely lives at only one address. The same guide may answer at a URL with and without a trailing slash, with tracking parameters, under an old path that was never redirected, as a Markdown copy for language model tools, as JSON for a client-side search index, and in a second language. Two link relations sort this out. canonical says which of the duplicates is the preferred one. alternate says which other documents are the same page in another language or another format.
The canonical relation
The canonical link relation is defined in RFC 6596, published in April 2012, and the WHATWG HTML standard refers to it for the meaning of rel="canonical". Its central rule is that the target must identify content that duplicates the current page or is a superset of it. A search engine that accepts the link may index only the target, merge signals such as links into it, and show it as the URL in results.
| The RFC allows the target to | The RFC advises against |
|---|---|
| Be a relative URL | More than one canonical link for the same page |
| Point to the page itself | A target that permanently redirects elsewhere |
| Be on a different host or domain | A target that itself names a different canonical |
| Use a different scheme, such as https for an http page | A target that returns a 4xx error |
| Be a view-all page that contains this page's content | Page 1 as the canonical of pages 2 and 3 of a series, which would discard their content |
Before adding a canonical link, the RFC recommends checking that a permanent redirect could not be used instead. A redirect removes the duplicate; a canonical link only asks applications to treat it as one.
How Google treats canonical links
Google describes canonicalization as a choice it makes from several signals, none of which is required and none of which it presents as binding. It ranks them by strength:
| Method | Strength as described by Google |
|---|---|
| Redirect | Strong signal that the target should become canonical |
| rel="canonical" link element or Link header | Strong signal that the named URL should become canonical |
| Inclusion in a sitemap | Weak signal |
- The link element is accepted only in the head of the HTML document.
- Google recommends absolute URLs, in the element and in the header, even though the RFC permits relative ones.
- Google advises against using robots.txt or noindex to choose a canonical within a site. A URL blocked by robots.txt cannot be fetched, so its canonical link cannot be seen at all.
- With hreflang annotations, Google recommends naming a canonical page in the same language, and it prefers URLs that belong to hreflang clusters when choosing canonicals.
Link headers for files without a head
RFC 8288 (October 2017, Web Linking) defines the HTTP Link header, which carries the same relations as HTML link elements. It is the only way to give a canonical or alternate link to a PDF, a plain text file, a JSON file or a Markdown copy of a page, and it can be added in server or CDN configuration without touching the files. Google's canonicalization documentation uses it for exactly this case.
HTTP/1.1 200 OK
Content-Type: text/markdown; charset=UTF-8
Link: <https://docs.example.com/guides/sitemaps>; rel="canonical"A response may carry several Link header lines, or one line with several links separated by commas; the two forms mean the same thing.
The alternate relation
The WHATWG HTML standard defines rel="alternate" by the attributes that accompany it. With hreflang set to a language different from the page's own, the target is a translation. With type set to a media type, the target is a reformulation of the current document in that format. With type set to application/rss+xml or application/atom+xml, the target is a feed, which is how browsers and feed readers find feeds. The two attributes can be combined.
Format alternates are how a page points to its machine-readable copies: type="text/markdown" for the Markdown version described in markdown versions of pages and llms-full.txt, or type="application/json" for a JSON rendering of the same content. Language alternates follow the hreflang rules set out in language alternates in sitemaps, which apply equally to link elements: fully qualified URLs, every version listing every version including itself, and links returned in both directions.
The HTML standard also treats alternate as transitive: if a page lists two alternates, they are alternates of each other as well. Consumers tell a translation from a reformulation by the hreflang and type attributes, which is one reason to always include them.
A bilingual documentation page
The English guide at https://docs.example.com/guides/sitemaps has a Korean translation at https://docs.example.com/ko/guides/sitemaps, a JSON rendering used by the site's search, and a Markdown copy. Its head carries one canonical link pointing to itself, three language links, and two format links.
<link rel="canonical" href="https://docs.example.com/guides/sitemaps">
<link rel="alternate" hreflang="en" href="https://docs.example.com/guides/sitemaps">
<link rel="alternate" hreflang="ko" href="https://docs.example.com/ko/guides/sitemaps">
<link rel="alternate" hreflang="x-default" href="https://docs.example.com/guides/sitemaps">
<link rel="alternate" type="application/json" href="https://docs.example.com/guides/sitemaps.json">
<link rel="alternate" type="text/markdown" href="https://docs.example.com/guides/sitemaps.md">The Korean page repeats the three hreflang links unchanged, but its canonical link points to the Korean URL, not the English one. A translation is not a duplicate, and naming the English page as its canonical would ask search engines to drop the Korean page.
The same links can be sent as headers on the HTML response instead of being written into the page. This is convenient when templates are hard to change, but a site should use one method or the other for each kind of link, so that the two never disagree.
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Link: <https://docs.example.com/guides/sitemaps>; rel="canonical"
Link: <https://docs.example.com/guides/sitemaps>; rel="alternate"; hreflang="en"
Link: <https://docs.example.com/ko/guides/sitemaps>; rel="alternate"; hreflang="ko"
Link: <https://docs.example.com/guides/sitemaps>; rel="alternate"; hreflang="x-default"
Link: <https://docs.example.com/guides/sitemaps.json>; rel="alternate"; type="application/json"
Link: <https://docs.example.com/guides/sitemaps.md>; rel="alternate"; type="text/markdown"Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Canonical link placed in the body | Google accepts the element only in the head | Move it into the head of the HTML the server sends |
| Two canonical links with different targets | Applications cannot tell which is meant and may ignore both | One canonical per page |
| Translation canonicalized to the English page | The translation may be dropped from results | Each language version names itself |
| Pages 2 and 3 of a series canonicalized to page 1 | Their content may be treated as duplicate and discarded | Each page names itself, or all name a view-all page |
| Canonical target redirects or returns 404 | The link is improper, and applications fall back on their own heuristics | Point to the final URL that returns 200 |
| Duplicate URL blocked in robots.txt | The crawler never sees the canonical link on it | Leave duplicates crawlable, or redirect them |
See also
- Markdown versions of pages and llms-full.txtThe llms.txt proposal asks sites to offer a clean Markdown copy of useful pages at the page's own URL with .md added, and to announce it with standard link relations. llms-full.txt, a single file holding a whole site, is a platform convention outside the proposal.
- Language alternates in sitemapsA sitemap can tell search engines that pages are translations or regional versions of each other, using xhtml:link elements with hreflang values inside each url entry. Every version lists every version, itself included.
- Page-level rules: the robots meta tag and X-Robots-Tagrobots.txt decides whether a URL is fetched; the robots meta tag and the X-Robots-Tag header decide what a search engine may do with a page or file once it has been fetched, such as leaving it out of the index or showing no snippet.
- Publishing JSON files: media type, encoding and parsingThe rules a published JSON file has to follow under RFC 8259, how to serve it over HTTP, and how to write, read, format and validate it with Python's json module without producing output that other parsers reject.
- Where site files live: the root path and /.well-known/Clients find machine-readable site files at fixed addresses: some at the root of each host, some under the /.well-known/ prefix of RFC 8615, and some inside every page. Where a file sits also decides which URLs it can speak for.
References
- Ohye, M. and Kupke, J. RFC 6596: The Canonical Link Relation. IETF, April 2012. rfc-editor.org
- Nottingham, M. RFC 8288: Web Linking. IETF, October 2017. rfc-editor.org
- WHATWG. HTML Living Standard, section 4.6.8: link types alternate and canonical. html.spec.whatwg.org
- Google Search Central. How to specify a canonical URL with rel="canonical" and other methods. developers.google.com
- Google Search Central. Tell Google about localized versions of your page. developers.google.com