Custom Domains and TLS on Cloudflare Pages
Every Cloudflare Pages project gets a *.pages.dev address with a valid certificate the moment it is created. Putting the site on your own domain adds two pieces: a DNS record that routes the name to the project, and a certificate for that name issued and renewed automatically. When both zones and records are on Cloudflare, it takes a minute. When they are not — or when an old record, a CAA policy or a redirect loop gets in the way — it becomes the most confusing part of the deploy.
This guide covers attaching apex and www domains, how certificates are issued, which SSL settings matter for a static site, HTTPS enforcement, and the fixes for the failures people hit most. It is part of Cloudflare Pages Edge Caching Setup.
Prerequisites
- A Pages project with at least one successful deployment.
- Control of the domain's DNS, ideally as a zone in the same Cloudflare account.
- Knowledge of any existing records for the names you are about to use.
Zone on Cloudflare or Not
The setup depends on where the domain's DNS lives.
Zone on Cloudflare. In the Pages project, open Custom domains, choose Set up a custom domain and enter the name. Pages creates a proxied CNAME to project.pages.dev — flattened at the apex — and requests a certificate. Nothing else is needed.
DNS elsewhere. Add a CNAME at your DNS provider from the subdomain to project.pages.dev, then add the domain in the Pages dashboard. Pages validates the record and issues a certificate. Apex domains cannot be attached this way because most providers cannot put a CNAME at the apex.
Apex and www Together
Most sites want both example.com and www.example.com to work, with one redirecting to the other. Attach both names to the Pages project, then add one redirect so readers and search engines see a single canonical host. A zone-level Redirect Rule is the cleanest place for it:
When incoming requests match: Hostname equals "www.example.com"
Then: Dynamic redirect to concat("https://example.com", http.request.uri.path)
Status 301, preserve query string
A _redirects rule cannot do this, because its sources are paths, not hostnames. Make sure the canonical host matches what the site generator uses for canonical URLs and sitemaps — a mismatch sends search engines conflicting signals. Redirect options in detail are in Configuring Redirects on Cloudflare Pages.
How the Certificate Is Issued
Pages requests a certificate for each custom domain from one of the certificate authorities Cloudflare uses, validates control of the name through the DNS record, and renews it automatically before expiry. You never upload or rotate anything.
Two things can stop issuance:
- CAA records. A CAA record restricts which authorities may issue for the domain. If the zone has one that allows only a single authority, Pages cannot get a certificate from the others. Add CAA entries for the authorities Cloudflare lists in its documentation, or remove the restriction.
- Conflicting records. An existing A or AAAA record for the same name, left over from a previous host, takes precedence or confuses validation. Delete it before adding the domain.
A third, rarer cause is DNSSEC left enabled at the registrar with keys from a previous DNS provider after moving the zone; resolvers then reject every answer for the domain. Check that the DS record at the registrar matches the one Cloudflare shows, or remove it and re-enable DNSSEC from Cloudflare.
The dashboard shows the domain's status as it moves from "Verifying" to "Active". If it stays in verification for more than 15 minutes with the zone on Cloudflare, one of the two causes above is almost always responsible.
SSL Settings That Matter
The zone's SSL/TLS encryption mode (Off, Flexible, Full, Full (strict)) controls how Cloudflare connects to an origin server. Pages is served by Cloudflare itself, so the mode does not change how the Pages site is served, but it does affect every other proxied record in the zone. Set it to Full (strict) so no other hostname silently falls back to unencrypted origin connections.
Then enforce HTTPS for readers:
- Always Use HTTPS redirects every
http://request tohttps://at the edge. - HSTS tells browsers to use HTTPS for the domain for a set period without trying HTTP first. Enable it with a short
max-agefirst, confirm nothing breaks, then raise it; see Enabling HSTS and Preload Safely. - Minimum TLS version of 1.2 is a safe default. TLS 1.3 is enabled automatically.
You can also set HSTS in _headers for the Pages project, which keeps it versioned with the site. Setting it in both places is harmless as long as the values match.
The redirect chain on a first visit is short, but it is still two round trips before the page begins to load. Keep every internal link, sitemap entry and canonical tag on the final https:// canonical host, and list that host in any marketing or social profiles, so most visitors arrive without any redirect at all.
Keeping pages.dev Out of Search Results
The project's *.pages.dev address keeps working after you attach a custom domain, and it serves the same content. If search engines discover it, they may index duplicates. Two options:
- Add a
_headersrule that sendsX-Robots-Tag: noindexon thepages.devhost only —_headerssupports host-specific rules with a full URL as the path, for examplehttps://project.pages.dev/*. - Add a Redirect Rule in a Cloudflare Access or Worker layer that redirects
pages.devrequests to the canonical domain.
Both keep preview URLs usable for reviewers. The first is simpler and does not affect branch previews on *.project.pages.dev, which should also carry noindex.
Measured Impact
A company moved its developer documentation from a self-managed Nginx server with manually renewed certificates to Cloudflare Pages with an apex custom domain. Certificate-related incidents, which had happened twice in the previous year when renewals failed, dropped to zero. The www redirect moved from an Nginx config to a single Redirect Rule, the pages.dev host was marked noindex, and a site audit two months later found no duplicate-host URLs indexed.
Pitfalls & Rollback
- Flexible SSL mode. It connects to origins over HTTP; for any non-Pages record in the zone, that means unencrypted traffic and potential redirect loops.
- Apex with external DNS. Not supported; move the zone or use a subdomain.
- HSTS preload too early. Preloading is hard to undo; wait until every subdomain serves HTTPS.
- Deleting the domain from Pages to "retry". It also removes the DNS record; note the record first.
- Rollback: remove the custom domain from the project and restore the previous DNS record; the site remains on
pages.dev.
Conclusion
On Cloudflare Pages, custom domains and TLS are automatic when the zone is on Cloudflare: add the name, Pages creates the record and certificate, and renewals take care of themselves. Attach both apex and www with a Redirect Rule to one canonical host, set the zone to Full (strict) with Always Use HTTPS and HSTS, keep pages.dev out of search results, and check old records and CAA policies first whenever a domain gets stuck.
FAQ
Can I use an apex domain with Cloudflare Pages?
Yes, if the domain's DNS zone is on Cloudflare. Pages creates a flattened CNAME at the apex pointing to the project. If the domain uses another DNS provider, only subdomains can be attached, via a CNAME record you create at that provider.
How long does certificate issuance take for a Pages custom domain?
Usually a few minutes when the zone is on Cloudflare. For external DNS it can take longer because the certificate authority must validate the CNAME, and a CAA record that does not allow Cloudflare's certificate authorities will block issuance entirely.
Which SSL/TLS mode should I use for a Pages site?
The zone SSL mode affects traffic between Cloudflare and origins, and Pages is served by Cloudflare itself, so the mode matters mostly for other records in the zone. Use Full (strict) for the zone and enable Always Use HTTPS and HSTS for the site.
Why does my custom domain show a 522 or certificate error after setup?
Usually because an old DNS record for the same name still points elsewhere, or because a CAA record blocks issuance. Delete conflicting A, AAAA or CNAME records, check CAA records, and re-trigger validation from the Pages dashboard.
Related
- Parent: Cloudflare Pages Edge Caching Setup — the full Cloudflare setup.
- Configuring Redirects on Cloudflare Pages — path-level redirects.
- Migrating from Cloudflare Pages to Workers Static Assets — moving the domain to a Worker.
- Enabling HSTS and Preload Safely — enforcing HTTPS in browsers.
- Automating Eleventy Deployments on Cloudflare Pages — the deploy pipeline in front of the domain.