Sub topic 10
Canonical URLs and duplicate content
This chapter assumes you know how HTTP status codes and redirects work, can read HTML links, and can follow short Python snippets. It builds on the earlier chapters on fetching and URL discovery.
Why duplicates happen
A search engine identifies a page by its URL. The same content often sits behind many URLs:
https://example.com/guideandhttps://example.com/guide/http://andhttps://versions of the same pagewww.example.comandexample.com- Tracking parameters such as
?utm_source=news - Sort or filter parameters such as
?sort=price
If the engine indexed every variant, results would show the same page several times, and the crawler would waste fetches on pages it already has. So the pipeline groups near-identical URLs into a cluster and picks one representative, the canonical URL.
Signals that point to a canonical
The engine weighs several signals. None is an absolute command.
| Signal | How you set it | Strength |
|---|---|---|
| Redirect | A 301 from the duplicate to the preferred URL | Strong |
| `rel="canonical"` link | A tag in the page head | Strong hint |
| Sitemap listing | Only list the preferred URL | Weak hint |
| Internal links | Always link to the preferred form | Moderate |
| HTTPS over HTTP | Serve and link the secure version | Moderate |
A canonical tag looks like this:
<link rel="canonical" href="https://example.com/guide">Place it in the <head> of every variant, including the canonical page itself. Use an absolute URL. A self-referencing tag on the preferred page removes ambiguity.
Normalizing URLs yourself
Before comparing content, a crawler cleans URLs with simple rules. This cheap step removes many duplicates without fetching anything.
from urllib.parse import urlsplit, urlunsplit, parse_qsl, urlencode
TRACKING = {"utm_source", "utm_medium", "utm_campaign"}
def normalize(url):
parts = urlsplit(url)
scheme = parts.scheme.lower()
host = parts.netloc.lower()
path = parts.path or "/"
query = sorted(
(k, v) for k, v in parse_qsl(parts.query) if k not in TRACKING
)
return urlunsplit((scheme, host, path, urlencode(query), ""))
print(normalize("HTTPS://Example.com/guide?utm_source=a&b=2&a=1#top"))This prints https://example.com/guide?a=1&b=2. It lowercases the scheme and host, drops tracking parameters, sorts the remaining ones, and removes the fragment.
Normalization is a guess. Paths are case-sensitive on many servers, so the code leaves them alone. Dropping a parameter that actually changes content would merge distinct pages.
Detecting duplicate content
Different URLs can serve identical or nearly identical text. Engines compare content after text extraction. An exact match can use a hash of the cleaned text. Near-duplicates need fuzzier methods, such as comparing sets of word shingles (overlapping runs of words).
import hashlib
def fingerprint(text):
cleaned = " ".join(text.lower().split())
return hashlib.sha256(cleaned.encode("utf-8")).hexdigest()
a = fingerprint("Crawlers follow links.")
b = fingerprint("crawlers follow links.")
print(a == b)This prints True, because whitespace and case differences are removed before hashing.
Common errors
- Canonical pointing to a redirect or error page. The engine may ignore the tag. Point it at a URL that returns 200.
- Conflicting signals. A canonical tag naming one URL while the sitemap and internal links favor another. The engine picks by its own weighing.
- Canonical tag blocked by robots.txt. If the crawler cannot fetch the page, it never sees the tag.
- Canonicalizing different content. Pointing page 2 of a list at page 1 can hide the items that appear only on page 2.
- Using noindex as a canonical tool. Noindex removes a page. It does not consolidate signals.
Exercise
A shop's product page is reachable at /shoes?color=red&utm_source=ad and /shoes?color=red. The tracking parameter does not change the page. Name two ways to steer the engine toward the second URL.
Show answer
Add <link rel="canonical" href="https://example.com/shoes?color=red"> to both pages, and 301-redirect or consistently avoid linking the tracking version. Listing only the clean URL in the sitemap and linking to it internally also helps.