Skip to content

www to apex redirect not working on Cloudflare: the DNS record www needs

The redirect from www.example.com to example.com does nothing, times out, or throws a DNS error. Nine times out of ten the www hostname has no proxied DNS record, so the edge never gets the chance to redirect. Here is the check and the fix.

A Cloudflare Redirect Rule can only fire on requests that actually reach Cloudflare’s edge. If www.example.com has no DNS record, browsers get NXDOMAIN and no rule on earth will run; if the record exists but is grey-cloud (DNS only), the request goes straight to your origin and the edge rule is skipped. The fix is almost always the same: give www a proxied DNS record — a CNAME to the apex is the usual choice — and then let a Single Redirect send it to the apex with path and query intact.

What’s happening

Order of operations matters here. A redirect can come from four places: the browser cache (a previously stored 301, replayed offline), the Cloudflare edge (Always Use HTTPS, then Redirect Rules — evaluated before cache and origin, but only for proxied hostnames), the origin server, and the CMS. For a www redirect, everything hinges on step zero: DNS resolution. The edge answers for a hostname only when that hostname has a record in the zone and the record is proxied (orange cloud). No record means the browser cannot connect at all. A DNS-only record means Cloudflare hands out your origin IP and steps out of the path — your Redirect Rule is never evaluated, and now the origin must answer for www itself, including presenting a certificate that covers it.

When the record is proxied, the record’s content barely matters for redirect purposes: the edge terminates the connection, evaluates rules, and issues the 301 before ever contacting the origin. That is why a placeholder address works fine for redirect-only hostnames.

What to check first

  • Does www resolve, and to what? Cloudflare anycast IPs mean proxied; your origin IP means DNS only; nothing means the record is missing.
  • Does the zone have a redirect for www at all — Single Redirect, Bulk Redirect entry, or a leftover Page Rule?
  • Are you testing through a browser that has already cached a 301 or an HSTS entry from earlier experiments?

How to verify

Start with DNS, because everything else depends on it:

dig +short www.example.com A
dig +short www.example.com AAAA

Proxied records return Cloudflare edge addresses; a missing record returns nothing. Then walk the chain and confirm one clean hop:

curl -sIL https://www.example.com/ | grep -iE 'HTTP/|location|server|cf-ray'

You want a single 301 with server: cloudflare and a cf-ray, whose location is the apex — then a 200. Check that deep links survive the hop with path and query preserved:

curl -sI 'https://www.example.com/pricing?utm_source=news' | grep -iE 'HTTP/|location'

If www is grey-cloud, see what the origin does when it receives the request directly:

curl -skI --resolve www.example.com:443:203.0.113.10 https://www.example.com/ | grep -iE 'HTTP/|location'

The fix

  1. Create the www record and proxy it. In DNS, add www as a CNAME to example.com with the orange cloud on. If you never want www traffic to be able to reach the origin, a placeholder A 192.0.2.1 or AAAA 100:: works too — the edge answers before the address is ever used. Record-type choices are covered in the DNS record cookbook.
  2. Write the Single Redirect. Rules, then Redirect Rules: match custom expression http.host eq "www.example.com", action Dynamic redirect, expression concat("https://example.com", http.request.uri.path), status 301, preserve query string enabled. The dashboard’s wildcard form — source https://www.example.com/*, target https://example.com/${1} — does the same job. Free zones get 10 Single Redirects (25 on Pro, 50 on Business, 125 on Enterprise), so one canonical-host rule fits comfortably; patterns and bulk lists are compared in Redirect Rules: bulk vs dynamic.
  3. Check rule order. Single Redirects evaluate top-down and only the first match runs. A broad rule above (a catch-all maintenance redirect, a country redirect) can swallow www traffic before your rule sees it. Move the host-canonicalization rule up.
  4. Confirm certificate coverage for deep hostnames. The Universal certificate covers example.com and *.example.com, so www.example.com is fine — but www.sub.example.com is not covered, and browsers fail the TLS handshake before any redirect can happen. Multi-level hostnames need Advanced Certificate Manager or a flattened naming scheme.
  5. Retest clean. Purge the zone cache if an old redirect was cached, then rerun the curl walk and finish in a fresh private window.

When this fix is wrong

  • www must stay DNS-only (some panels and legacy constraints pin it to the origin). Then do the redirect at the origin instead: a vhost or ServerAlias for www plus a certificate that includes it, issuing the same single 301.
  • You actually want the opposite direction. If marketing, existing backlinks, and analytics all standardized on www, redirect the apex to www instead — do not fight history for aesthetics.
  • The symptom is a TLS error, not a missing redirect. Fix certificate coverage first; no rule runs on a connection that never completes a handshake.

Common mistakes

  • Assuming Cloudflare redirects www automatically. It does not; without a record, the hostname simply does not exist.
  • Adding the record but leaving it grey-cloud, then spending an hour debugging a rule that is never evaluated.
  • Testing in a browser that cached the old behaviour. A stored 301 or HSTS entry misrepresents the current state; curl tells the truth.
  • Leaving a legacy Forwarding URL Page Rule in place. Redirect Rules are evaluated first, so results look inconsistent while both exist. Migrate and delete the Page Rule.
  • Forgetting query-string preservation, which silently strips campaign parameters from every inbound ad click.

Prevention

Make the pair of records part of your standard zone template: apex and www, both proxied, plus one host-canonicalization Redirect Rule created the day the zone is set up. Document which host is canonical, and keep a four-variant curl walk (http/https times www/apex) in the go-live checklist so a missing record is caught before launch. Cloudflare’s Rules documentation is the reference for the expression syntax.

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.