In short
The <link rel="canonical"> tag tells search engines which URL represents a page. To be of any use, it must be unique, placed in the <head>, written as an absolute URL (https://example.com/chemin/) and point to an indexable page that responds 200 on the protocol and domain you have chosen. The task is to audit these points on every template, then fix the way the site builds the URL: that is where almost all errors originate.
Why it matters
Google groups pages with identical or very similar content and chooses, for each group, a canonical URL: the one that will be crawled most often and shown in the results. The canonical you declare is a strong signal for this choice, but a hint, not a rule. A faulty canonical is ignored or, worse, followed to the wrong URL.
Google recommends absolute URLs rather than relative ones: a relative path (/produits/sencha/) takes the host of the page that carries it. If a staging copy, an old domain, an http:// version or a mirror gets crawled, every page declares itself canonical on that wrong host. Typical errors:
http://instead ofhttps://, because the application, behind a proxy or CDN, thinks it is receiving HTTP;- the staging domain or the server's internal address (
http://10.0.0.12:8080/…); - two conflicting tags, one from the theme, the other from a plugin;
- a canonical placed in the
<body>, or pushed into the body because an invalid element (img,iframe,div) closes the<head>earlier than expected for the HTML parser; - a canonical pointing to a redirected URL, a 404 or a
noindexpage.
Google only accepts the canonical in the <head> and, for canonicalization, does not use tags that carry an hreflang, lang, media or type attribute.
What it improves
- Google's choice of canonical Your declarations are followed instead of ignored.
- Signal consolidation Links and popularity are concentrated on a single URL per piece of content.
- Accident prevention A crawled mirror or staging site can no longer declare itself canonical.
- Consistency for AI answers Engines cite the indexed URL; a correct canonical prevents a variant from being selected. No practice guarantees a citation.
- Rankings No direct effect; the effect comes through consolidation.
How to check
1. One page, in the initial HTML:
curl -s https://example.com/produits/sencha-ariake/ | grep -oiE '<link[^>]*rel="?canonical"?[^>]*>'
curl -s -o /dev/null -D - https://example.com/livre-blanc.pdf | grep -i "^link:"
Expected result: a single line, such as <link rel="canonical" href="https://example.com/produits/sencha-ariake/">.
2. After rendering: in the URL Inspection tool, "Test live URL" then "View tested page", HTML tab. The canonical must be the same as in the initial HTML. The "Page indexing" section of the indexed URL shows "User-declared canonical" and "Google-selected canonical".
3. The whole site, from the sitemap, with this script (adjust the address and the number of URLs):
#!/usr/bin/env bash
SITE="https://example.com"
curl -s "$SITE/sitemap.xml" | grep -oE '<loc>[^<]+' | sed 's/<loc>//' | head -300 | while read -r url; do
html=$(curl -s -L "$url")
count=$(printf '%s' "$html" | grep -oiE '<link[^>]*rel="?canonical"?[^>]*>' | wc -l | tr -d ' ')
canonical=$(printf '%s' "$html" | grep -oiE '<link[^>]*rel="?canonical"?[^>]*>' | head -1 | grep -oiE 'href="[^"]*"' | sed -E 's/href="([^"]*)"/\1/')
status="OK"
[ "$count" -eq 0 ] && status="MISSING"
[ "$count" -gt 1 ] && status="MULTIPLE"
[ "$count" -ge 1 ] && [[ "$canonical" != https://* ]] && status="NOT_ABSOLUTE_OR_HTTP"
[ "$count" -ge 1 ] && [[ "$canonical" == https://* ]] && [[ "$canonical" != "$SITE"/* && "$canonical" != "$SITE" ]] && status="OTHER_DOMAIN"
[[ "$canonical" == *"#"* ]] && status="FRAGMENT"
[ "$status" = "OK" ] && [ "$canonical" != "$url" ] && status="TO_OTHER_URL"
printf '%s\t%s\t%s\n' "$status" "$url" "$canonical"
done | sort > canonicals.tsv
TO_OTHER_URL is not necessarily an error, but a URL in the sitemap should declare itself. Every other status calls for a fix.
In Screaming Frog, the Canonicals tab offers the filters "Missing", "Multiple", "Multiple Conflicting", "Canonical Is Relative", "Non-Indexable Canonical", "Contains Fragment URL", "Invalid Attribute In Annotation" and "Outside <head>" out of the box. Enable JavaScript rendering to compare the initial and rendered HTML.
How to implement
Before you start
Settle on a single URL form and apply it everywhere: protocol (https), host (with or without www), trailing slash, letter case, no tracking parameters. Every other form redirects with a 301 to this one: see standardize HTTP/HTTPS and www/non-www (opens in a new tab) and standardize letter case and trailing slashes (opens in a new tab).
Expected form:
<head>
<meta charset="utf-8">
<title>Sencha Ariake, loose-leaf Japanese green tea</title>
<link rel="canonical" href="https://example.com/produits/sencha-ariake/">
</head>
Place the canonical early in the <head>, before third-party scripts that might inject invalid elements.
Apache
Versions Apache 2.4.
For non-HTML files (PDFs, documents), the canonical goes in the HTTP Link header. Example that makes the PDF version the reference for a document also published as .docx:
<Files "livre-blanc.docx">
Header set Link "<https://example.com/livre-blanc.pdf>; rel=\"canonical\""
</Files>
Behind a proxy or load balancer, the application must know that the original request was over HTTPS; otherwise it generates http:// canonicals. If Apache is itself the reverse proxy:
RequestHeader set X-Forwarded-Proto "https"
ProxyPreserveHost On
Reload with sudo apachectl configtest && sudo systemctl reload apache2.
Nginx
Versions Nginx 1.18 and later.
Header for a PDF, and passing the protocol and host on to the application:
location = /livre-blanc.docx {
add_header Link '<https://example.com/livre-blanc.pdf>; rel="canonical"';
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
}
For PHP-FPM, include fastcgi_params; passes HTTPS; check that fastcgi_param HTTPS $https if_not_empty; is present. Reload with sudo nginx -t && sudo systemctl reload nginx.
Cloudflare
Versions Cloudflare dashboard, all plans, November 2026.
In the "Flexible" SSL/TLS mode, Cloudflare connects to your server over HTTP: the application thinks the page is on http:// and may write that into the canonical. The default mode, "Automatic SSL/TLS", picks the most secure mode the origin accepts, so without a certificate on the origin it can stay on Flexible. Install a certificate on the origin, then, in the SSL/TLS overview, choose "Custom SSL/TLS" and "Full (strict)". A Worker can also rewrite the canonical with HTMLRewriter, but fix the source instead.
AWS
Versions Elastic Load Balancing and CloudFront consoles, November 2026.
Behind an Application Load Balancer or CloudFront, the application often receives HTTP. The ALB adds X-Forwarded-Proto; with CloudFront, forward the CloudFront-Forwarded-Proto or Host header in the origin request policy, then configure the application to trust the proxy (see the Laravel, Symfony, WordPress and Drupal sections). Otherwise, hard-code the base URL in the application's configuration.
WordPress
Versions WordPress 6 and 7; Yoast SEO 20 and later; Rank Math 1.0.
Settings > General: "WordPress Address (URL)" and "Site Address (URL)" must usehttps://on the final domain. WordPress builds the canonical from these values.- Behind a proxy or CDN that terminates TLS, add this to
wp-config.php, before the linerequire_once ABSPATH . 'wp-settings.php';:
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}
- WordPress only writes a canonical on single content items (posts, pages); Yoast SEO or Rank Math add it to archives. Keep a single source: if the theme also writes a tag, remove it.
- The "Canonical URL" field in the "Advanced" tab of Yoast SEO or Rank Math must contain a full URL.
Shopify
Versions Shopify admin and Online Store 2.0 themes, November 2026.
Themes write <link rel="canonical" href="{{ canonical_url }}"> in layout/theme.liquid. Check that there is only one tag (an SEO app sometimes adds a second one) and that a primary domain is set in Settings > Domains, with the other domains redirecting to it.
PrestaShop
Versions PrestaShop 1.7.8, 8 and 9.
In Shop Parameters > Traffic & SEO, fill in the shop domain and the SSL domain, and enable SSL on all pages in Shop Parameters > General. The canonical is written by the theme (templates/_partials/head.tpl, from $page.canonical): an SEO module must not add a second one.
Magento
Versions Magento Open Source and Adobe Commerce 2.4.6 to 2.4.9.
In Stores > Configuration > General > Web, the secure base URLs (Base URLs (Secure)) must use https://, with "Use Secure URLs on Storefront" set to "Yes". Canonicals for products and categories are enabled in Stores > Configuration > Catalog > Catalog > Search Engine Optimization.
Drupal
Versions Drupal 10 and 11; Metatag module 2.x.
Core writes a canonical on content pages; the Metatag module lets you set its pattern. Behind a proxy, in settings.php:
$settings['reverse_proxy'] = TRUE;
$settings['reverse_proxy_addresses'] = ['10.0.0.1'];
Replace the address with your proxy's, then clear the caches (drush cr).
Wix
Versions Wix Editor and Wix Studio, November 2026.
Wix automatically generates an absolute self-referencing canonical for every page. Only change it for a real duplicate, in the page's advanced SEO settings, with a full URL.
Squarespace
Versions Squarespace 7.1, November 2026.
Squarespace adds canonical tags itself, for example on blog tag and category pages, does not let you edit the pages' source code, and redirects connected domains to the primary domain. Check that the primary domain is the one you want, and that the built-in squarespace.com domain is not the one appearing in the canonicals.
Webflow
Versions Webflow interface (Site settings), November 2026.
In Site settings > SEO, fill in "Global canonical tag URL" with the protocol, the www if your default domain uses it, and no trailing slash (https://www.example.com): Webflow appends each page's slug to it. Do not inject a second canonical through Google Tag Manager. Publish to apply.
Laravel
Versions Laravel 10 to 13.
In .env, APP_URL=https://example.com. In the Blade template:
<link rel="canonical" href="{{ url()->current() }}">
url()->current() returns the absolute URL without the query string. Behind a proxy, the application must trust the X-Forwarded-* headers; how you declare this depends on the version. If the protocol is still wrong, force it in production in AppServiceProvider::boot() with URL::forceScheme('https');.
Laravel 11 to 13
In bootstrap/app.php:
->withMiddleware(function (Middleware $middleware): void {
$middleware->trustProxies(at: '*');
})
Restrict at: to your proxies' addresses if they are fixed (at: ['10.0.0.0/8']).
Laravel 10
In app/Http/Middleware/TrustProxies.php, set the $proxies property:
protected $proxies = '*';
Restrict it to your proxies' addresses if they are fixed (['10.0.0.0/8']). Laravel 10 has not received security fixes since February 2025.
Symfony
Versions Symfony 6.4, 7.4 and 8.
Use url(), which produces an absolute address, not path(), which produces a path:
<link rel="canonical" href="{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}">
Behind a proxy, in config/packages/framework.yaml:
framework:
trusted_proxies: '127.0.0.1,REMOTE_ADDR'
trusted_headers: ['x-forwarded-for', 'x-forwarded-proto', 'x-forwarded-host', 'x-forwarded-port']
Next.js
Versions Next.js 15 and 16, App Router and Pages Router.
App Router
Define metadataBase in app/layout.js; relative paths in the metadata are then converted to absolute URLs:
// app/layout.js
export const metadata = {
metadataBase: new URL('https://example.com'),
};
// app/produits/[slug]/page.js
export async function generateMetadata({ params }) {
const { slug } = await params;
return {
alternates: { canonical: `/produits/${slug}` },
};
}
Without metadataBase, a relative path causes a build error. Since Next.js 15.2, metadata resolved late can be streamed at the end of the document: check in the inspection tool that the canonical is present in the rendered HTML, and prefer metadata resolved before streaming.
Pages Router
next/head has no base URL: write the full URL, from an environment variable.
// pages/produits/[slug].js
import Head from 'next/head';
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL; // https://example.com
export default function Produit({ produit }) {
return (
<>
<Head>
<link rel="canonical" href={`${siteUrl}/produits/${produit.slug}`} key="canonical" />
</Head>
{/* product page content */}
</>
);
}
Nuxt
Versions Nuxt 4 (and Nuxt 3, end of life since July 2026).
// app/app.vue (app.vue at the root in Nuxt 3) or a page, inside <script setup>
const route = useRoute();
const siteUrl = useRuntimeConfig().public.siteUrl; // https://example.com
useHead({
link: [{ rel: 'canonical', href: `${siteUrl}${route.path}` }],
});
Declare siteUrl in runtimeConfig.public in nuxt.config.ts, with one environment variable per environment.
Astro
Versions Astro 5 to 7.
Set site: 'https://example.com' in astro.config.mjs, then in the layout:
---
const canonical = new URL(Astro.url.pathname, Astro.site);
---
<link rel="canonical" href={canonical.href} />
Common mistakes
- Relative paths: valid HTML, but Google recommends absolute URLs, and a crawled mirror then declares itself canonical.
http://in the canonical of an HTTPS site: a symptom of an application that ignores the proxy, or of Cloudflare in "Flexible" mode.- Staging domain in the canonical: base URL copied from the test environment's configuration.
- Two tags: theme and SEO plugin, or a server-side tag plus a tag injected by JavaScript or Google Tag Manager.
- Canonical pointing to a URL that redirects or returns 404: it must target the final URL returning 200 directly.
- Canonical with a fragment (
#avis): Google asks you not to designate a fragment as canonical. - Canonical to the homepage on every page: every page loses its chance of being indexed.
- Only checking the initial HTML: a script can change the tag at render time.
Frequently asked questions
Should the canonical tag contain an absolute URL?
Yes, that is Google's recommendation. A relative URL is valid HTML, but it takes the host of the page that carries it: if a staging site, an http version or a mirror gets crawled, every page declares itself canonical there. So write the full URL, with https, the right domain and the exact form of the path, for example https://example.com/produits/sencha-ariake/, and generate it from a base URL set in the configuration.
What happens if a page has two canonical tags?
Two canonical tags pointing to different URLs send conflicting signals: Google may ignore them and choose the canonical version itself. Even when identical, they show that two sources write the tag, which often ends up diverging. The typical case is a theme and an SEO plugin that each write one. Keep a single source and check with curl or a crawler that only one tag remains.
Why does my canonical start with http when the site uses https?
An http canonical on an https site almost always comes from an application that cannot see the original protocol. Behind a CDN, proxy or load balancer, it receives the request over HTTP and builds the URL with it. Configure the application to trust the proxy's X-Forwarded-Proto header, switch Cloudflare from Flexible to Full (strict) mode, or set the base URL to https.
Can you declare a canonical for a PDF file?
Yes, with the HTTP Link header, since a PDF has no head tag. The header points to the full URL of the reference document with the canonical relation, and is configured in Apache, Nginx or the application. Google says it only supports this method for web search results. It is mainly useful when the same document exists in several formats or at several addresses, to designate the version to index.
Resources
- Google Search Central: How to specify a canonical URLdevelopers.google.com
- Google Search Central: What is canonicalization?developers.google.com
- RFC 6596: The Canonical Link Relationrfc-editor.org
- MDN: rel attributedeveloper.mozilla.org
- Next.js: metadataBase and alternatesnextjs.org
- Laravel: trusted proxies and Symfony: behind a proxylaravel.com
- Webflow: canonical tags and Wix: changing canonical tagshelp.webflow.com
Updated on
Topic checklist (PDF)