On this page
A page can declare a canonical URL in both its HTTP Link header and its HTML. If those URLs differ, the response sends conflicting signals. Before a technical SEO audit assigns a cause, inspect both declarations for the same request and response, then check redirects and other page variants.
This guide shows a reproducible inspection workflow. The code has not been executed here. RFC 8288 and Google Search Central documentation could not be consulted in this environment, so crawler-specific handling and current guidance should be checked before drawing conclusions about a particular search engine.
Key Takeaways
- Capture the response headers and HTML body from the same request, without automatically following redirects first.
- Compare every canonical declaration, not just the first header or HTML element. Resolve relative targets in their proper context before comparing them.
- Inspect redirect hops and the final page separately. Repeat the inspection for variants that matter, such as mobile and regional responses.
- Do not assume that the header or HTML declaration always wins. Align the signals and verify how the crawler under investigation handles them.
Understand the conflict
An HTTP response can carry a Link header with a canonical relation. An HTML document can also contain a link element whose rel attribute includes canonical. Both identify a preferred URL, but canonicalization is a crawler decision, not a guarantee that a search engine will select the URL you declare.
For example, a response might identify one product URL in its header and a different URL in its HTML. The difference could originate in separate layers: a CDN or server configuration may emit the header, while an application template emits the HTML element. That is a diagnostic possibility, not proof of where any particular conflict originated.
Look for differences in scheme, hostname, path, trailing slash, query string, and encoding. Do not assume that a difference is harmless merely because both URLs appear to load the same content.
Advertisement
Inspect the header and HTML for the same URL
You need curl and Python 3. Replace the example URL below with a URL you own or are authorized to inspect. Use a fresh request and do not follow redirects for this first capture:
curl -sS --compressed \
-D response-headers.txt \
-o response-body.html \
-w 'HTTP status: %{http_code}\nEffective URL: %{url_effective}\n' \
'https://www.example.com/products/widget'
The command saves headers and body from the request and prints its status and effective URL. Inspect the saved headers:
cat response-headers.txt
Look for all Link fields and their rel parameters, as well as the status, Location, and content type. A response can contain more than one Link field or multiple link-values in a field. Do not split a field naively on commas: commas can occur within quoted values or URL references. Also distinguish any informational response headers from the final response headers in the capture.
Next, inspect the saved HTML. This small script reports canonical link elements, their raw href values, and whether they were encountered inside the document head:
python3 - <<'PY'
from html.parser import HTMLParser
from pathlib import Path
class CanonicalInspector(HTMLParser):
def __init__(self):
super().__init__()
self.in_head = False
self.base_hrefs = []
self.canonicals = []
def handle_starttag(self, tag, attrs):
if tag == 'head':
self.in_head = True
attributes = dict(attrs)
if tag == 'base' and attributes.get('href') is not None:
self.base_hrefs.append((attributes['href'], self.in_head))
if tag == 'link':
relations = (attributes.get('rel') or '').lower().split()
if 'canonical' in relations:
self.canonicals.append((attributes.get('href'), self.in_head))
def handle_endtag(self, tag):
if tag == 'head':
self.in_head = False
inspector = CanonicalInspector()
inspector.feed(Path('response-body.html').read_text(encoding='utf-8', errors='replace'))
print('Base hrefs:', inspector.base_hrefs)
print('Canonical hrefs:', inspector.canonicals)
PY
The script is an inspection aid, not a browser or crawler simulator. It assumes UTF-8 and may not reflect browser recovery from malformed markup. Check the response's declared encoding and inspect unusual markup with appropriate tools. It reports raw HTML, not elements inserted or changed by JavaScript. If rendering could change the result, inspect both the original response and the rendered document using the crawler or browser workflow relevant to your investigation.
Compare the targets only after resolving relative references. For HTML, the document URL and any applicable base element affect resolution. HTTP Link values have their own context and may include an anchor parameter; check RFC 8288 when interpreting a nontrivial header. Absolute canonical URLs are easier to inspect, but do not silently rewrite ambiguous values when reporting a finding.
If the status is a redirect or the body is not the expected HTML document, do not compare that response's header with a different page's HTML and call it a same-response conflict. Continue with the redirect investigation instead.
Follow redirects without losing the evidence
A redirect response and its destination are separate responses. Each hop can have its own headers, and the final HTML can contain another canonical declaration. Capture the chain as a second step:
curl -sS --compressed -L \
-D redirect-headers.txt \
-o final-body.html \
-w 'Final HTTP status: %{http_code}\nFinal URL: %{url_effective}\n' \
'https://www.example.com/products/widget'
Review redirect-headers.txt in order. Record each response status, its Location, and any canonical Link fields. Associate each header block with the URL requested at that hop; if that is unclear, request each hop individually without -L. Then inspect final-body.html using the same script, changing its filename.
A canonical on an intermediate redirect is not the same evidence as a canonical in the final page's response. Check for redirect loops, unexpected host or scheme changes, and a final URL that differs from the intended canonical target. An error page or blocked request is not a reliable representation of the intended page either.
Advertisement
Check multiple declarations and page variants
A conflict is not limited to one header versus one HTML element. Check for duplicate or differing canonical relations across multiple Link fields, multiple canonical elements in the HTML, and declarations added during rendering. Record all of them, including malformed or missing targets, before changing configuration.
Then repeat the capture for variants that could produce different responses:
- HTTP and HTTPS, alternate hostnames, trailing-slash versions, and relevant query-string URLs.
- Mobile and desktop request headers, if the server varies its response by user agent.
- Language or region variants, if request headers, cookies, or paths select different content.
- Anonymous and authorized states, where appropriate; avoid exposing credentials or private content in audit artifacts.
- Responses served through different CDN or cache paths, if you can reproduce them safely.
Record the URL, request headers, time of inspection, status, redirect chain, response headers, and canonical elements for each capture. A changed user-agent string alone does not reproduce a search crawler's complete behavior. Cache state and content negotiation can also make a conflict intermittent, so repeat a request when results are inconsistent.
Resolve the disagreement
- Identify the preferred URL for each content variant with the site owner. Confirm that the destination is intended to be accessible and represents the content being consolidated.
- Trace each declaration to its owner: application templates, server configuration, CDN rules, plugins, or other middleware. Inspect configuration rather than assuming the last layer you changed is responsible.
- Remove unintended duplicates and align the final response's header and HTML declarations with the intended target. Review redirect behavior separately.
- Repeat the captures for the affected URLs and variants. Check both raw and rendered HTML if rendering is relevant.
- Consult the current documentation and reporting tools for the search engine being investigated. An aligned declaration does not, by itself, prove which URL that crawler selected.
Common Mistakes
- Comparing different responses. A browser's final HTML and an earlier redirect's header may refer to different URLs.
- Using only a HEAD request. It does not provide the HTML body needed for this comparison, and server behavior may differ from a GET request.
- Checking only one declaration. Duplicate elements or additional
Linkfields can hide the conflict. - Assuming a universal precedence rule. Crawler behavior can differ; verify the relevant crawler's current guidance.
- Treating raw and rendered HTML as identical. Client-side changes require a separate rendering check.
- Normalizing away meaningful differences. Query parameters, paths, and hostnames may distinguish real content variants.
Best Practices
Keep a small evidence table for each tested URL: requested URL, response status, redirect destination, header canonical target, HTML canonical target, and request conditions. Note whether each value came from raw or rendered HTML.
Prefer one intentional, consistent canonical target for a given response, and document which layer owns each declaration. Recheck after changes to templates, infrastructure, or CDN configuration. Where crawler behavior matters to the audit, corroborate the response evidence with that crawler's current documentation and diagnostics.
Conclusion
Start with one captured response, inventory its canonical signals, and only then expand to redirects and variants. Aligning the declarations removes an avoidable ambiguity; determining the URL a particular crawler selects requires crawler-specific verification.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.