What you're doing and why it's painful
If someone visits www.yourdomain.com, they should land on yourdomain.com — the same page, no dead end. Without a redirect, you have duplicate content (bad for SEO) and confused visitors.
Cloudflare can handle this entirely at the edge, before the request ever hits your server. The pain: the redirect lives in a different part of the dashboard depending on your Cloudflare plan, the SSL setting has to be right or you'll get redirect loops, and getting the $1 wildcard wrong means your redirect strips the path.
Prerequisites
- Your domain is using Cloudflare DNS (orange cloud icon)
- You have access to the Cloudflare dashboard for that zone
Option A — Redirect Rules (recommended, free plan)
Cloudflare's Redirect Rules are the modern replacement for Page Rules. They work on all plans including free.
- Log in to Cloudflare dashboard
- Select your domain
- Go to Rules → Redirect Rules
- Click Create rule
- Name it:
Redirect www to apex - Under When incoming requests match..., set:
- Field:
Hostname - Operator:
equals - Value:
www.yourdomain.com
- Field:
- Under Then..., set:
- Type:
Dynamic - Expression:
concat("https://yourdomain.com", http.request.uri.path) - Status code:
301
- Type:
- Click Deploy
The http.request.uri.path variable preserves the path — so www.yourdomain.com/pricing correctly redirects to yourdomain.com/pricing.
Option B — Page Rules (older method, still works)
- Go to Rules → Page Rules
- Click Create Page Rule
- URL:
www.yourdomain.com/* - Setting: Forwarding URL
- Status: 301 - Permanent Redirect
- Destination:
https://yourdomain.com/$1 - Click Save and Deploy
The $1 captures whatever comes after www.yourdomain.com/ and appends it to the destination. www.yourdomain.com/pricing → yourdomain.com/pricing.
Step 2 — Check your SSL/TLS setting
A common cause of redirect loops: your SSL setting is too low.
- Go to SSL/TLS → Overview
- Make sure it's set to Full or Full (strict) — not Flexible
With Flexible SSL, Cloudflare connects to your origin over HTTP, which can cause loops when your origin also tries to redirect HTTP to HTTPS.
Step 3 — Verify the DNS entry for www
Make sure your www record exists in Cloudflare DNS and is proxied (orange cloud):
- Go to DNS → Records
- You should see a
wwwCNAME or A record pointing to your domain or IP - The cloud icon should be orange (proxied) — if it's grey (DNS only), Cloudflare's redirect rules won't apply
If there's no www record, add one:
- Type:
CNAME - Name:
www - Target:
yourdomain.com - Proxy: on (orange cloud)
Testing the redirect
curl -I https://www.yourdomain.com
Look for HTTP/2 301 and location: https://yourdomain.com/ in the response headers. If you see a 200 instead, the redirect hasn't deployed yet or there's a DNS/proxy issue.
Common errors and gotchas
Redirect loop Almost always caused by Flexible SSL. Switch to Full or Full (strict) under SSL/TLS settings.
www.yourdomain.com not resolving at all
No www DNS record exists. Add a proxied CNAME pointing www to your apex domain.
Path not preserved — redirect lands on homepage regardless of URL
Using $1 in Page Rules but the wildcard isn't matching. Make sure your Page Rule URL is www.yourdomain.com/* (with the /* at the end). Alternatively, switch to Redirect Rules which use the expression syntax above.
Redirect not showing as 301
Cloudflare may cache the first response. Wait a few minutes and test in incognito to avoid browser cache. Or use curl which ignores browser caching.