Skip to content

WordPress canonical conflicts behind Cloudflare: site URL mismatch and CDN hostname canonicals

The rel=canonical tag points at the wrong host, WordPress 301s against your edge rules, or Google starts indexing cdn.example.com. All three trace back to the same thing: WordPress and the edge disagree about the canonical URL. Here is how to realign them.

WordPress computes canonical URLs — both the rel="canonical" tag and the redirect_canonical() 301 — from its siteurl and home settings. Cloudflare canonicalizes independently, from whatever Redirect Rules you wrote. When the two disagree, you get redirect chains, loops, or canonical tags that point somewhere your redirects contradict; and when a CDN hostname serves full HTML, the site quietly exists twice. The fix is always the same move: decide the one true URL form, set WordPress to it, and make every other layer either agree or redirect to it.

What’s happening

For any given page request, the answer can be shaped by four layers in order: the browser cache (replaying stored 301s), the Cloudflare edge (Always Use HTTPS, then Redirect Rules — proxied hostnames only), the origin server, and WordPress last — which both emits the canonical tag in the HTML and issues its own 301 when the requested URL differs from what siteurl/home say it should be, labelled x-redirect-by: WordPress.

Two conflict patterns dominate. First, site URL mismatch: the edge rule canonicalizes to https://www.example.com while siteurl still says https://example.com (or worse, http://). The edge pushes visitors one way, WordPress pushes back — a chain if they eventually agree on something, a loop if they do not, and canonical tags that contradict the redirects either way. Second, CDN hostname canonicals: a pull-zone or vanity CDN hostname (cdn.example.com) is meant to serve assets but also answers for pages. If siteurl was ever pointed at it, canonical tags and internal links start advertising the CDN host; even if not, the duplicate HTML is out there for crawlers to find.

What to check first

  • What WordPress believes: siteurl and home, plus whether WP_HOME/WP_SITEURL constants in wp-config.php override the database.
  • What the edge enforces: which host and scheme your Redirect Rules push toward.
  • What the HTML says: the actual rel="canonical" on a few templates (home, a post, an archive), because SEO plugins can override core.
  • Whether the CDN hostname serves HTML at all.

How to verify

Read the canonical tag and the redirect behaviour for a sample page:

curl -s https://example.com/sample-page/ | grep -io '<link rel="canonical"[^>]*>'
curl -sI https://example.com/sample-page/ | grep -iE 'HTTP/|location|x-redirect-by|cf-ray'

x-redirect-by: WordPress on a 301 means the application, not the edge, moved you — compare its location with what your Redirect Rules target. Walk the chain from the non-canonical host and count hops:

curl -sIL https://example.com/sample-page/ | grep -iE 'HTTP/|location'

Check whether the CDN hostname answers with pages instead of assets:

curl -sI https://cdn.example.com/sample-page/ | grep -iE 'HTTP/|content-type|location'

content-type: text/html with a 200 is the duplicate-site problem. Finally, confirm what the origin does without the edge (-k for an Origin CA certificate):

curl -skI --resolve www.example.com:443:203.0.113.10 https://www.example.com/sample-page/ | grep -iE 'HTTP/|location|x-redirect-by'

The fix

  1. Decide the canonical form once — scheme plus host plus slash policy — and write it where the team will see it. Every following step implements that decision.
  2. Align WordPress. wp option update siteurl https://www.example.com and the same for home; if WP_HOME/WP_SITEURL are defined in wp-config.php, fix them there instead, because constants win over the database.
  3. Sweep stored URLs. wp search-replace 'https://example.com' 'https://www.example.com' --all-tables --dry-run first, then for real — page builders and older content store absolute URLs that keep resurfacing in links and canonicals.
  4. Make the CDN hostname assets-only. Best: stop routing page paths through it at all. Belt and suspenders: a Redirect Rule on cdn.example.com sending any path without a file extension back to the canonical host, so stray HTML requests 301 home instead of duplicating the site. If you run a separate-hostname CDN by design, the setup rules are in CDN setup: Cloudflare, Bunny, KeyCDN.
  5. Align the SEO plugin. Clear any hand-forced canonical overrides that were papering over the mismatch; with siteurl correct, core and plugin output agree on their own.
  6. Re-check agreement with the edge. The Redirect Rules’ target and WordPress’s canonical must be byte-identical in host, scheme, and slash. Then purge the zone cache so old HTML with stale canonical tags stops being served; Cloudflare’s fundamentals docs cover purge access if you are new to the dashboard.

When this fix is wrong

  • The wrong canonical came in with a database import — staging or development URLs in a dump. That is a migration search-replace across all tables, not a settings fight.
  • Multisite with domain mapping. Per-site URLs live elsewhere and blanket search-replace can corrupt the network; treat each mapped site separately.
  • Hosts differ on purpose (per-language or per-brand hostnames). Then canonicals should differ per host; conflicts inside one host are still fixed the same way.

Common mistakes

  • Editing siteurl in the database while the constants are defined. Nothing changes, and the diagnosis spirals.
  • Forcing canonicals in the SEO plugin instead of fixing siteurl. The tag now disagrees with WordPress’s own 301s and with internal links — you have added a third opinion, not resolved two.
  • Leaving the CDN hostname serving HTML and just adding a canonical tag. Crawlers still fetch and weigh the duplicates; redirect or block them instead.
  • Fixing everything except old cached pages. Stale HTML at the edge keeps advertising the old canonical until purged.
  • Chasing the loop in the browser. Cached 301s misreport which layer moved you; attribute hops with curl and x-redirect-by.

Prevention

Set WP_HOME and WP_SITEURL explicitly in wp-config.php on every managed build so a stray settings screen or importer cannot drift the site URL. Keep the canonical decision documented next to the edge rules that implement it, and add a two-line check to deploys: fetch one page, compare the canonical tag with the redirect target. When chains do sneak in, the hunt method in finding and fixing redirect chains applies unchanged behind Cloudflare.

If redirects keep fighting each other across Cloudflare, the origin, and the CMS, Stack Harbor untangles that chain as part of Managed Cloud Ops — book an infrastructure review.