Sub topic 3
Normalizing URLs to expose hidden duplicates
This chapter assumes you can read a crawl export in a spreadsheet or pandas, know basic HTTP status codes and redirects, and can read simple Python. A crawler records each URL as a separate row. If a site serves one page under several addresses, the export shows several pages. Normalizing URLs groups those variants under one key so you can see the real duplicates.
The working model
A normalization key is a simplified form of a URL. Two URLs that share a key are candidates for being the same page. The key is a tool for analysis. It does not change the site.
These variants commonly split one page into many rows:
| Variant | Example | Normalization rule |
|---|---|---|
| Protocol | `http://` and `https://` | Force `https` |
| Host case | `Example.com` and `example.com` | Lowercase the host |
| Trailing slash | `/shoes/` and `/shoes` | Remove the trailing slash, except on the root |
| Tracking parameters | `?utm_source=news` | Drop known tracking keys |
| Parameter order | `?a=1&b=2` and `?b=2&a=1` | Sort the parameters |
| Fragment | `/shoes#reviews` | Remove the fragment |
Host case is safe to lowercase because hostnames are case-insensitive. Paths are case-sensitive on many servers, so this chapter leaves path case alone.
A normalization function
from urllib.parse import urlsplit, urlunsplit, parse_qsl, urlencode
TRACKING = {"utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content", "gclid", "fbclid"}
def normalize(url):
parts = urlsplit(url)
host = parts.netloc.lower()
path = parts.path or "/"
if path != "/" and path.endswith("/"):
path = path.rstrip("/")
query = sorted((k, v) for k, v in parse_qsl(parts.query) if k not in TRACKING)
return urlunsplit(("https", host, path, urlencode(query), ""))
print(normalize("http://Example.com/shoes/?utm_source=news&color=red"))
# https://example.com/shoes?color=redThe function does these steps in order:
- Splits the URL into scheme, host, path, query and fragment.
- Lowercases the host and gives an empty path the value
/. - Strips a trailing slash from any path longer than the root.
- Removes tracking parameters and sorts the rest.
- Rebuilds the URL with
httpsand an empty fragment.
Applying it to a crawl
Add the key as a column, then group by it. Any key with more than one URL is a variant group.
import pandas as pd
df = pd.read_csv("crawl.csv") # columns: url, status
df["key"] = df["url"].map(normalize)
variants = df.groupby("key")["url"].agg(list)
print(variants[variants.str.len() > 1])Run this after the function above is defined. Look at the status column for each group. A variant that returns a 301 to the preferred URL is already handled. A variant that returns 200 is a live duplicate.
Common errors
- Treating the key as proof. Two URLs with the same key are only candidates. Confirm that the site serves the same content, for example by comparing content hashes, which a later chapter covers.
- Dropping parameters that change content. Parameters such as
page=2orcolor=redoften change what the page shows. Only remove keys you have confirmed are tracking-only. - Forcing
httpson a site that serves both differently. If thehttpversion returns different content, the merge hides a real problem. - Removing slashes on a site where they matter. Some servers treat
/shoesand/shoes/as different resources. Check the response before trusting the rule.
What to do with the groups
Sort groups by size. Large groups usually point to a systematic cause, such as links that omit the trailing slash or a campaign tool that appends parameters. Note which variants the site links to internally and which appear in the sitemap. Later chapters use that information to pick a preferred URL.
Exercise
A crawl contains http://Example.com/boots/ and https://example.com/boots?gclid=abc. Both return status 200. After running normalize(), do they share a key, and what does that tell you?
Show answer
Both become https://example.com/boots, so they share a key. This shows they are candidates for being one page served at two live addresses. Before calling them duplicates, compare their body content and check whether either redirects, has a canonical link, or is blocked.