Skip to content

Apex to www on Cloudflare: the Free-plan Redirect Rules recipe

Send example.com to www.example.com with one Single Redirect rule, a proxied apex record, and path plus query preserved. The whole recipe works on the Free plan — here is the setup, the verification, and the traps.

Redirecting example.com to www.example.com on Cloudflare takes one Single Redirect rule and one proxied DNS record for the apex, and it works entirely on the Free plan — no Workers, no Page Rules, no paid add-ons. The rule matches the apex hostname, 301s to the www form with the path and query string carried over, and the edge answers before your origin is ever contacted. The two things people miss: the apex record must exist and be orange-clouded, and the target must preserve the path or every deep link lands on the homepage.

What’s happening

The edge evaluates in a fixed order: Always Use HTTPS first, then Bulk Redirects, then Single Redirects — all before the cache and the origin, and only for hostnames whose DNS record is proxied. So the apex needs a proxied record for the rule to run at all. When the origin genuinely serves the apex, point the record at it as usual; when the origin only knows www, a placeholder A 192.0.2.1 or AAAA 100:: is the documented pattern — the edge issues the redirect before the address is ever dialled.

Plan limits, for planning: Free zones get 10 Single Redirect rules (25 Pro, 50 Business, 125 Enterprise) and 20 Bulk Redirect entries. One host-canonicalization rule uses a single slot. Regex operators (matches, regex_replace) need Business or Enterprise, but this recipe needs none of them.

What to check first

  • Does the apex have a DNS record, and is it proxied? Grey cloud means the rule never runs.
  • Does the origin vhost and certificate cover www? The final destination must return 200 over HTTPS.
  • Is a legacy Forwarding URL Page Rule already doing (or half-doing) this? Redirect Rules evaluate first, so leftovers create confusing test results — see migrating Page Rules to modern rules.
  • Does the CMS agree? A WordPress siteurl still set to the apex will bounce visitors straight back.

How to verify

After creating the rule, walk all four entry variants and count hops:

for u in http://example.com https://example.com http://www.example.com https://www.example.com; do
  printf '%-30s ' "$u"
  curl -sIL -o /dev/null -w '%{num_redirects} hops -> %{url_effective}\n' "$u/"
done

Inspect the apex hop itself — it should be a 301 from the edge (server: cloudflare, a cf-ray, no cf-cache-status) pointing at the www form:

curl -sI 'https://example.com/pricing?utm_source=news' | grep -iE 'HTTP/|location|server|cf-ray|cf-cache-status'

And confirm the origin itself answers for www, bypassing the edge (-k if it runs an Origin CA certificate):

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

The fix

  1. Give the apex a proxied record. DNS, add or edit the apex record, orange cloud on. Real origin IP if the origin serves the apex; placeholder A 192.0.2.1 / AAAA 100:: if it should never be reached. Cloudflare handles the CNAME-at-apex restriction with flattening, so a CNAME works here too.
  2. Create the Single Redirect. Rules, Redirect Rules, create rule. Wildcard form: request URL https://example.com/*, target https://www.example.com/${1}, status 301. Expression form does the same: filter http.host eq "example.com", dynamic target concat("https://www.example.com", http.request.uri.path), preserve query string enabled. Both are Free-plan features; the Rules documentation covers the syntax.
  3. Decide who handles HTTP entries. Keeping Always Use HTTPS on is the simple default: http://example.com then costs two hops (scheme first, host second), which is acceptable for most sites. If you want a single hop from every entry point, disable Always Use HTTPS and widen the rule filter to (http.host eq "example.com") or (http.request.scheme eq "http") with the same https://www target — one rule, one hop, but now that rule alone owns HTTPS enforcement, so document it.
  4. Align the origin and CMS. Set the WordPress siteurl/home (or the app’s base URL) to https://www.example.com, and remove any origin rule that redirects www back to the apex.
  5. Retire the old mechanisms. Delete the Forwarding URL Page Rule and any origin .htaccess host redirect so exactly one owner remains.

When this fix is wrong

  • Your history lives on the apex. If backlinks, indexed pages, and campaigns all use example.com, canonicalize in the other direction instead — consistency beats preference.
  • The apex must stay unproxied because another service consumes it directly. Then the origin has to issue this redirect, and this recipe does not apply.
  • You are consolidating dozens of hostnames or domains. Host-by-host Single Redirects stop scaling; use a Bulk Redirect list, as compared in Redirect Rules: bulk vs dynamic.

Common mistakes

  • No apex record at all. The rule is correct but browsers get NXDOMAIN; nothing runs without DNS resolution.
  • A static target instead of ${1} or concat. Every deep link then lands on the homepage, and search engines treat it as a soft mass-redirect to one URL.
  • Using 302. Temporary redirects do not consolidate ranking signals to the www form; use 301 for a permanent host decision.
  • Testing in a browser with a cached 301 or HSTS pin from earlier attempts. Verify with curl, then with a fresh private window.
  • Leaving the Page Rule active “just in case”. Two mechanisms for the same hop eventually disagree; keep one.

Prevention

Bake the pattern into your zone template: both hostnames proxied, one canonical-host rule, Always Use HTTPS on, and the four-variant curl loop in the go-live checklist. Note the canonical host in the project docs so the next engineer extends the decision instead of reversing it.

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.