Cloudflare Pages

Redirect Your Apex Domain to Cloudflare Pages

Cloudflare Pages serves your site from www.yourdomain.com via a CNAME to your-project.pages.dev. The apex (naked) domain needs separate handling. Here's the right approach for both DNS scenarios.

Set Up Apex Redirect
TL;DR

Cloudflare Pages requires a CNAME to <project>.pages.dev, but apex domains can't use CNAME per RFC 1034. If Cloudflare is your DNS, CNAME flattening handles this automatically. If you use another DNS (Route53, GoDaddy, etc.), use ApexToWWW to redirect apex→www. Free. Want it the other way round (www→apex)? Jump straight there.

Two Scenarios for Cloudflare Pages Custom Domains

Whether you can use Cloudflare's native solution or need a third-party redirect depends entirely on where your DNS is hosted, not where your site is hosted. Cloudflare Pages happily serves your site no matter who runs your authoritative DNS. The apex problem is purely a DNS problem.

Cloudflare is your nameserver

Use Cloudflare's CNAME flattening. Add a CNAME on the apex pointing to your-project.pages.dev and Cloudflare flattens it to A/AAAA records at query time. It just works.

DNS lives elsewhere

Route53, GoDaddy, Namecheap, Google Domains, etc. don't all support CNAME flattening (or call it ALIAS/ANAME). Use ApexToWWW for a free apex→www 301 redirect.

Step-by-Step: Cloudflare Pages with Non-Cloudflare DNS

This is the most common case: you've shipped a Cloudflare Pages site but your domain's DNS is still at the registrar where you bought it. Here is the complete setup.

  1. Open your Cloudflare Pages project in the Cloudflare dashboard, then go to Custom domains.
  2. Click "Set up a custom domain" and enter www.yourdomain.com. Cloudflare verifies ownership and provisions an SSL certificate.
  3. Copy the CNAME target Cloudflare gives you. It looks like your-project.pages.dev.
  4. At your DNS provider, add a CNAME record: name www, value your-project.pages.dev. TTL: 1 hour or auto.
  5. Add an A record on the apex: name @, value 65.21.184.101.
  6. Add an AAAA record on the apex: name @, value 2a01:4f9:c012:a304::1.
  7. Verify with curl -I http://yourdomain.com — you should see 301 Moved Permanently with Location: https://www.yourdomain.com/. Then load www.yourdomain.com to confirm the Pages site is live.

DNS Records Summary

Type Name Value
CNAME www your-project.pages.dev
A @ 65.21.184.101
AAAA @ 2a01:4f9:c012:a304::1

The CNAME points www at your Cloudflare Pages project. The A and AAAA records send apex traffic to ApexToWWW, which 301-redirects to https://www.yourdomain.com.

When NOT to Use ApexToWWW for Cloudflare Pages

We're a focused tool, not a hammer. Skip ApexToWWW if:

  • Your DNS is on Cloudflare. Use CNAME flattening. It's native, faster, and one less external dependency. Add a CNAME on @ pointing to your-project.pages.dev and Cloudflare flattens it automatically.
  • You want the apex to serve your site directly (no redirect, no www in the URL). For that you need ALIAS/ANAME records, which only some DNS providers offer. Cloudflare DNS does this transparently via flattening; if you can move DNS to Cloudflare, that's the cleanest path.
  • You're already using Cloudflare Workers for routing logic. Bind a route on the apex and write the redirect in code — one fewer moving part.

ApexToWWW is the right answer when you can't or won't move DNS to Cloudflare — for example, your team owns DNS in Route53 with Terraform, your registrar's DNS is managed by IT, or you simply prefer to keep nameservers and CDN separate.

Meta-Note: This Site Runs on Cloudflare Pages

Eat your own dogfood: apextowww.com itself runs on Cloudflare Pages. There's a CNAME on www pointing to apextowww-73y.pages.dev, and the apex redirect is handled by the same service we ship to you. The setup on this page is exactly what we do.

Wanted the Opposite? Cloudflare Pages Redirect WWW to Apex

If you landed here looking to redirect www.yourdomain.comyourdomain.com (the reverse direction), the setup is different — and Cloudflare gives you better tools than we do for that case, because the canonical destination is the apex.

You need two things:

  1. Resolve the apex to your Pages project. Add a CNAME on @ pointing to your-project.pages.dev, with Cloudflare DNS doing CNAME flattening (this is automatic when DNS is on Cloudflare). Or, if your DNS provider supports ALIAS/ANAME records, point ALIAS @ at your-project.pages.dev.
  2. Redirect www to the apex with a Cloudflare rule. The cleanest path is a Single Redirect Rule (under Rules → Redirect Rules):
    • Match: (http.host eq "www.yourdomain.com")
    • Action: Dynamic redirect to concat("https://yourdomain.com", http.request.uri.path)
    • Status code: 301
    On the legacy free plan you can use Page Rules instead with www.yourdomain.com/*https://yourdomain.com/$1, 301.

If you prefer code, a four-line Cloudflare Worker bound to www.yourdomain.com/* does the same job:

export default {
  fetch(req) {
    const url = new URL(req.url);
    url.hostname = "yourdomain.com";
    return Response.redirect(url.toString(), 301);
  }
};

Should you pick apex or www as canonical? Both work. www gives you CNAME-based routing, cookie isolation between subdomains, and the path of least friction on every modern host. Apex is shorter and feels more modern, at the cost of needing ALIAS/ANAME or CNAME flattening, and the historical "cookie bleed" issue where cookies on the apex are sent to every subdomain. For most projects the apex→www direction is the safer default — which is what the rest of this page describes — but if you really want www→apex on Cloudflare Pages, the rule above is the right answer. For other platforms, see the full redirect www to apex guide.

Other Platforms

The apex→www pattern is identical across most modern hosts. Same two records on the apex, only the www CNAME target changes:

FAQ

Does CNAME flattening work with email MX records?

Yes. CNAME flattening only resolves the apex A/AAAA queries; MX, TXT, SPF, DKIM, and DMARC records are unaffected. You can run Google Workspace or Fastmail on the apex while Cloudflare flattens the CNAME for HTTP traffic. The classic "CNAME at apex breaks email" problem doesn't apply to flattened records.

Can I combine the Cloudflare proxy (orange cloud) with ApexToWWW?

No. If the orange cloud is on for the apex A/AAAA records, Cloudflare terminates TLS at its edge and the request never reaches the ApexToWWW server, so the redirect can't happen. Either set the apex records to DNS only (gray cloud), or use Cloudflare's CNAME flattening if Cloudflare is your DNS provider. You can absolutely keep the orange cloud on for the www record — that's between Cloudflare and your Pages project and works fine.

What if I use Cloudflare Workers instead of Pages?

Same shape. Bind your Worker to www.yourdomain.com via a route, then handle the apex with either ApexToWWW or CNAME flattening. Workers can also do the redirect themselves — a four-line script returning a 301 from example.com to https://www.example.com bound to the apex route is a perfectly good alternative if you're already paying for the platform.

Is ApexToWWW compatible with DNSSEC?

Fully. ApexToWWW is just A and AAAA records on your apex. DNSSEC is signed by your DNS provider over the zone contents and doesn't care what the records resolve to. Sign the zone as you normally would; the redirect works regardless.

How do I redirect www to apex on Cloudflare Pages (the reverse direction)?

Point the apex at your Cloudflare Pages project — CNAME flattening on @ to your-project.pages.dev, or an ALIAS/ANAME record if your DNS provider supports one — then add a Cloudflare Redirect Rule matching http.host eq "www.yourdomain.com" that 301s to https://yourdomain.com. On the legacy free plan, a Page Rule (www.yourdomain.com/*https://yourdomain.com/$1, 301) does the same job. See the full walkthrough above.

Ship Your Cloudflare Pages Apex Redirect

Two DNS records, free forever, automatic SSL via Let's Encrypt. Works with any DNS provider that accepts A and AAAA records.

Get Started Free

Questions? hi@apextowww.com