Sub topic 12
Choosing the right canonical URL
This chapter assumes you can read a URL (scheme, host, path, query string, fragment). It also assumes you know what rel="canonical" does and where it goes in the HTML head, and that you recognize the common HTTP status codes such as 200 and 301. Here the question is which URL to name, and how to make the rest of your site agree with that choice.
What "right" means
A canonical tag is a hint. Search engines weigh it against other signals and may pick a different URL. Choosing well therefore means more than writing a tag. You pick the one URL you want shown in results, and you make your redirects, internal links, sitemaps, and tags all point at it.
The right canonical URL has four properties:
- It returns a 200 status and is not redirected or blocked.
- It is the version you would want a visitor to land on and share.
- It is stable. The same content always maps to the same URL.
- It is consistent with your site's conventions for scheme, host, trailing slash, and parameters.
Common variants and the usual choice
Most duplicate-content problems come from a small set of URL variants. The table gives the usual choice for each.
| Variant | Example | Usual canonical choice |
|---|---|---|
| Scheme | `http://` and `https://` | `https://` |
| Host | `example.com` and `www.example.com` | The one you picked, used everywhere |
| Trailing slash | `/shoes` and `/shoes/` | One convention for the whole site |
| Tracking parameters | `?utm_source=news` | The URL without them |
| Session identifiers | `?sid=8f3a` | The URL without them |
| Sort order | `?sort=price` | The default ordering |
| Filter combinations | `?color=red` | Depends on whether the filtered page has its own value |
| Pagination | `?page=2` | The page itself (self-canonical) |
| Print or alternate views | `/shoes/print` | The main view |
| Syndicated copies | A partner site republishing your article | The original article, possibly on another domain |
| Fragment | `#reviews` | The URL without it (fragments are not sent to the server) |
A decision flow
The flow below covers most cases. It starts with the strongest tool, which is to remove the duplicate entirely.
Redirects come first because they are the strongest signal. Use a canonical tag when the variant must stay reachable, for example a tracked campaign link or a sort order that users click.
Does the parameter change the content?
For each query parameter, ask whether removing it would change the main content a visitor sees.
utm_source,gclid,fbclid, and session IDs do not change the content. Drop them from the canonical.?sort=pricereorders the same items. Canonicalize to the default order.?page=2shows different items. Keep it, and make the page canonical to itself. Pointing page 2 at page 1 tells search engines that the items on page 2 are not worth indexing.?color=reddepends. If the red-shoes page has its own heading, copy, and inventory that people search for, it can be self-canonical. If it only filters a list without adding anything distinct, canonicalize to the unfiltered page.
Write the rule down once and encode it in code, not in each template.
Encoding the rule in code
This helper extends the earlier canonicalUrl function. It forces HTTPS, uses the www host, removes the fragment and the trailing slash, drops parameters that do not change content, and sorts the rest so that ?a=1&b=2 and ?b=2&a=1 produce the same result.
const CONTENT_PARAMS = new Set(["page", "color", "size"]);
function chooseCanonical(rawUrl) {
const url = new URL(rawUrl);
url.protocol = "https:";
url.hostname = url.hostname.toLowerCase();
if (!url.hostname.startsWith("www.")) {
url.hostname = "www." + url.hostname;
}
url.hash = "";
if (url.pathname.length > 1 && url.pathname.endsWith("/")) {
url.pathname = url.pathname.slice(0, -1);
}
const kept = [...url.searchParams.entries()]
.filter(([key]) => CONTENT_PARAMS.has(key))
.sort((a, b) => a[0].localeCompare(b[0]));
url.search = new URLSearchParams(kept).toString();
return url.toString();
}
console.log(
chooseCanonical(
"http://example.com/shoes/trail-running/?utm_source=a&page=2&color=red#reviews"
)
);
// https://www.example.com/shoes/trail-running?color=red&page=2This sketch deliberately does not lowercase the path. URL paths are case-sensitive on many servers, so changing the case in a tag can point to a URL that returns 404. If your site treats paths case-insensitively, lowercase the path in the same step as the redirect rule that enforces it.
Cross-domain canonicals
When you republish content on another domain, or a partner republishes yours, the copy can point at the original. The tag goes in the head of the copy and uses an absolute URL on the other domain.
<!-- Served at https://partner.example.net/articles/trail-shoe-guide -->
<link rel="canonical" href="https://www.example.com/guides/trail-shoe-guide">This only works when the pages are near-identical. If the partner rewrites the page substantially, the engine will probably treat them as different pages and ignore the hint.
Signals must agree
Search engines combine several signals to select the canonical. Redirects and rel="canonical" are strong signals. Inclusion in a sitemap and the URL you use in internal links are weaker ones. Using HTTPS and a clean URL also tips the balance. If the signals disagree, the engine picks for you, and the result is often not what you wanted.
Check that all of these name the same URL:
- The
rel="canonical"tag orLinkheader on the page. - The
<loc>entry in your XML sitemap. - The
hrefvalues of internal links. - The target of any 301 redirect from a variant.
- The
hreflangannotations, if you have them.
Self-referencing canonicals
Every indexable page should carry a canonical tag that points to its own clean URL. This protects you when someone links to the page with extra parameters, or a scraper copies it. Because the preferred URL is declared on the page itself, a variant that nobody anticipated still resolves to it.
The tag must be an absolute URL. A relative href such as /shoes/trail-running can be resolved against the wrong scheme or host, for example on a staging server or behind a proxy.
Exercise
A store serves https://www.example.com/shoes?sort=price&utm_campaign=spring and https://www.example.com/shoes?page=2. The site's canonical rule keeps page and drops sort and utm_*. What canonical should each page declare, and why not point the second page at /shoes?
Show answer
The first page should declare https://www.example.com/shoes, because sort order and campaign tracking do not change which items the page is about. The second page should declare https://www.example.com/shoes?page=2. It lists different items, and pointing it at /shoes would tell search engines to ignore those items.
Exercise
Your sitemap lists https://example.com/guide/, your canonical tag says https://www.example.com/guide, and internal links use https://www.example.com/guide/. Which signals conflict, and what is the fix?
Show answer
All three use different combinations of host and trailing slash. Pick one form, for example https://www.example.com/guide, and use it in the tag, the sitemap, and the internal links. Add a 301 redirect from the other forms to it.