Canonical Tags

Prevent duplicate content issues by declaring a canonical URL.

Syntax<link rel="canonical" href="https://example.com/page">

The canonical tag tells search engines which version of a page is the master when several URLs show similar content.

When you need it

  • Product pages reachable via multiple category paths.
  • URLs with tracking or filter parameters (?utm=...).
  • HTTP vs HTTPS or www vs non-www duplicates.
  • Printer-friendly or paginated variants.

How it works

Place a self-referencing canonical on every page. On duplicates, point the canonical to the preferred URL. Google treats it as a strong hint, not an absolute command.

Example

Example · html
<!-- On https://example.com/coffee/pour-over-guide?utm_source=news -->
<link rel="canonical"
  href="https://example.com/coffee/pour-over-guide">

When to use it

  • An e-commerce site adds self-referencing canonical tags to all product pages to prevent duplicate content from URL parameter combinations like ?color=red&size=M.
  • A blog that syndicates content to Medium sets the canonical URL on the Medium copy to point back to the original post, ensuring Google credits the source.
  • A developer consolidates HTTP/HTTPS and www/non-www variants by deploying canonical tags and 301 redirects to a single preferred URL format.

More examples

Self-referencing canonical tag

A self-referencing canonical prevents Google from choosing a parameterised or session-cookie URL as the preferred version of the page.

Example · html
<head>
  <!-- Every indexable page should declare its own canonical URL -->
  <link rel="canonical" href="https://example.com/shoes/trail-runner-pro/">

  <!-- Without this, Google may canonicalise a parameterised version:
       https://example.com/shoes/trail-runner-pro/?color=blue&sort=price
       and split link equity across both URLs -->
</head>

Canonical for syndicated content

Cross-domain canonical tags on syndicated copies consolidate ranking signals to the original publisher's URL rather than splitting them.

Example · html
<!-- On the Medium / partner syndication copy -->
<head>
  <!-- Tell Google the original source should rank, not this copy -->
  <link rel="canonical"
        href="https://example.com/blog/original-article/">
</head>

<!-- The original page uses a self-referencing canonical -->
<!-- <link rel="canonical" href="https://example.com/blog/original-article/"> -->

Audit canonical consistency with Python

Comparing each URL's canonical against the expected value programmatically catches parameterised or variant pages that declare the wrong canonical.

Example · bash
python3 - << 'EOF'
import requests
from bs4 import BeautifulSoup

urls = [
    "https://example.com/shoes/trail-runner-pro/",
    "https://example.com/shoes/trail-runner-pro/?color=blue",
]
for url in urls:
    soup = BeautifulSoup(requests.get(url, timeout=5).text, 'html.parser')
    canon = soup.find('link', rel='canonical')
    cv = canon['href'] if canon else '[MISSING]'
    match = 'OK' if cv == 'https://example.com/shoes/trail-runner-pro/' else 'MISMATCH'
    print(f"[{match}] {url} -> canonical: {cv}")
EOF

Discussion

  • Be the first to comment on this lesson.