Deploy Astro on Cloudflare Pages: GitHub to Custom Domain, Step by Step
11 November 2026 · Updated 28 November 2026 · 12 min read · by Yang Kai
Deploy an Astro site on Cloudflare Pages step by step: build settings, GitHub connection, custom domain, a starter redirects file, and the checks to run before you announce the launch.
Table inside: _redirects starter file + Cloudflare Pages settings table
Contents
- What you need before you start
- Step 1: Connect GitHub and create the project
- Step 2: Set the build settings
- Step 3: Verify the pages.dev deployment
- Step 4: Add your custom domain
- Step 5: Add redirects — the step everyone skips
- Step 6: Run the post-deploy checklist
- Working with previews, branches, and rollbacks
- Environment variables, per environment
- What to do next
Most first deploys fail for one of three boring reasons: a wrong build command, a Node version mismatch, or a domain pointed at the wrong place. None of them needs talent to avoid — just the steps in the right order, with the settings written down.
This guide takes an Astro project from a GitHub repo to a live site on your own domain: connect, build settings, first deploy, custom domain, redirects, and the post-deploy checks people skip. Every setting below is the one a standard Astro site needs, and the free-tier limits are Cloudflare’s published figures, checked November 2026.
One honest note before the steps: Cloudflare’s docs now steer brand-new projects toward Workers. Pages is still fully supported, still the simplest Git-connected route for a static site, and everything in this guide works today. Astro on Cloudflare: the complete setup explains when the Workers route is the better pick.
What you need before you start
- Your Astro project in a GitHub repository, building locally with
npm run build. If the local build fails, fix that first; no host can deploy a broken build. - A free Cloudflare account. No card is required for the Pages free plan.
- A domain you control — only needed for Step 4. If it is registered elsewhere, you can still use Pages; the cleanest path is pointing the domain’s nameservers at Cloudflare, which also gives you free DNS and SSL in the same dashboard.
- About 20 minutes, most of it waiting on the first build and DNS.
One decision to make now: use Pages, not Workers, for a standard static Astro site. Pages is the static-and-simple path with Git integration built in. That is what this guide uses.
Step 1: Connect GitHub and create the project
In the Cloudflare dashboard, go to Workers & Pages → Create application → Pages → Connect to Git. Authorise Cloudflare to access your GitHub account — you can scope it to a single repository — and select your Astro repo.
Cloudflare will try to guess your framework and pre-fill the build settings. Do not accept the guesses blindly; the next step verifies them, and a wrong build command is the number one cause of failed first deploys.
You should now see: your project listed in the dashboard, waiting on its first build configuration.
Note: Pages also offers Direct Upload — drag a built dist folder into the dashboard or push it with Wrangler. It is handy for a one-off test, but a Direct Upload project can never be switched to Git integration later; you would recreate the project to get push-to-deploy. For anything you plan to keep, connect Git now and save yourself the rebuild.
Step 2: Set the build settings
Match your project against this table before you save:
| Setting | Value for a standard Astro site |
|---|---|
| Framework preset | Astro |
| Production branch | main |
| Build command | npm run build |
| Build output directory | dist |
| Root directory | / (or your subfolder, if the site lives in one, e.g. site/) |
| Environment variable | NODE_VERSION set to match your local node -v |
Save and deploy. The first build takes a couple of minutes.
What the free plan gives you, from Cloudflare’s official Pages limits (checked November 2026): 500 builds per month, one build at a time, a 20-minute limit per build, up to 20,000 files per site and 25 MiB per single file, with unlimited bandwidth for the static files themselves. The Cloudflare Pages free tier entry tracks these numbers as Cloudflare changes them. For a content site, the builds-per-month figure is the only one you are ever likely to meet.
Note: if your build expects environment variables — an analytics ID, a form endpoint — add them under Settings → Environment variables for both production and preview. A variable that exists only on your laptop is the second most common cause of a first-deploy failure.
Step 3: Verify the pages.dev deployment
When the build finishes, Cloudflare gives you a your-project.pages.dev URL. Open it and click through several pages — the homepage, one blog post, a listing page, and a URL that does not exist, which should show your 404 page — before touching the domain.
Every deploy from now on works the same way: push to main, and Pages builds and publishes automatically. Pull requests and other branches get their own preview URLs, which is how you review changes safely before they go live.
If the build failed, read the log from the bottom up. The three usual causes:
- Node version mismatch — fix with the
NODE_VERSIONvariable from Step 2. - Package manager mismatch — delete any stray
yarn.lockorpnpm-lock.yamlif your project uses npm. - Missing files —
.envvalues and assets that exist on your machine but were never committed.
You should now see: your real site, fully navigable, on the pages.dev address.
Step 4: Add your custom domain
In your Pages project, open Custom domains → Set up a custom domain and enter your root domain. If the domain’s DNS is already on Cloudflare, the required record is created for you and SSL is issued automatically. Add the www version as a second custom domain too, then decide which one is canonical — the root domain is the common choice — and redirect the other to it (Step 5 covers how).
If your DNS lives elsewhere, Cloudflare shows you the exact CNAME record to add at your registrar. Propagation is usually minutes, occasionally hours. Do not announce your launch until both the root and www resolve over HTTPS without warnings.
While you are in settings, set your canonical URLs in Astro itself to match: the site: value in astro.config.mjs should be the final HTTPS domain. Sitemaps, canonical tags, and Open Graph URLs are all generated from it, so a leftover localhost or pages.dev value here will quietly poison your SEO — the honest Astro SEO guide lists what to check. Themes from the themes directory expose this as a single config value: change it once, rebuild, and verify the sitemap shows the real domain.
You should now see: your site on your own domain, over HTTPS, with the other version redirecting to it.
Step 5: Add redirects — the step everyone skips
Redirects are how you avoid breaking every link you have ever shared when URLs change. Cloudflare Pages reads a plain-text _redirects file placed in your Astro public/ folder (it gets copied into dist on build). One rule per line: source, destination, status code. The file can hold up to 2,000 static and 100 dynamic rules (Cloudflare’s published limit, checked November 2026) — far more than a content site needs.
Copy this starter into public/_redirects and edit it for your site:
# www → root domain (adjust if your canonical is www instead)
https://www.example.com/* https://example.com/:splat 301
# Old section names → new ones
/blog/old-category/* /blog/new-category/:splat 301
# Moved single pages
/about-us/ /about/ 301
/pricing-old/ /pricing/ 301
# Retired pages with no replacement → send to the closest relevant page, not the homepage
/old-product-review/ /themes/ 302
Three rules of thumb. Use 301 for permanent moves (almost everything) and reserve 302 for genuinely temporary ones. Redirect retired pages to the closest relevant page, not the homepage — visitors and search engines both prefer a useful landing. And test every rule after deploying by actually visiting the old URL; a redirect file with a typo fails silently.
Note: _redirects only fires for paths that do not match a real built file, so it will not override live pages. For forced rules there is an ! syntax, but reach for it rarely — needing it usually means a routing problem upstream.
Step 6: Run the post-deploy checklist
Deployment is not done when the homepage loads. Run through this list once per launch:
- Homepage, one article, one listing page and the 404 page all load over HTTPS
- Both root and
wwwwork, with one redirecting to the other -
sitemap-index.xmlloads and lists your real domain - A test push to a non-production branch produced a preview URL
- Every rule in
_redirectswas clicked and lands where intended - Analytics (if any) fires on the production domain, not the preview
Astro’s output is static files, so caching behaves predictably: HTML is revalidated, hashed assets are cached hard. You rarely need custom cache rules for a content site — leave Cloudflare’s defaults alone until you have a measured reason to change them. When the site grows dynamic pieces, handling forms and email on static sites covers the patterns that keep it static.
Working with previews, branches, and rollbacks
The Git integration keeps working for you after launch, and three habits make it pay off.
Review on preview URLs, not on production. Every pull request and non-production branch gets its own deployment at a unique URL. Send that link to anyone reviewing a change. If the preview is wrong, production never knew about it.
Keep the production branch sacred. Only merges to main should change the live site. Under Settings → Builds & deployments you can control which branches get preview deployments at all — turning previews off for stray branches keeps your build count for work that matters, which matters on a 500-builds-a-month free plan shared across every Pages project on the account.
Treat rollback as your undo button. Every deployment stays listed in the dashboard. If a bad deploy reaches production, open the last good deployment and choose Rollback to this deployment; the switch is effectively instant and buys you time to fix forward properly. Knowing this exists before you need it is the difference between a bad five minutes and a bad afternoon.
Environment variables, per environment
Pages keeps production and preview variables separate, and the separation cuts both ways. A form endpoint or analytics ID added only to production will be silently absent from every preview build, which is fine — until you test the form on a preview, conclude it works, and ship a production build where the variable was never added either.
The discipline: when you add a variable, add it to both environments in the same sitting, with the environment-appropriate value, and note it in your project readme. Variables your build needs (like NODE_VERSION or a public site URL) must exist before the first build that uses them; runtime-only values can be added any time, but a rebuild is needed before a static site picks up a changed build-time value — static files cannot read new settings after the fact.
Build succeeds locally, fails on Pages. Compare Node versions first, then check for files that exist on your machine but were never committed — .env values and uncommitted assets are the classic culprits.
Site deploys but shows an old version. Confirm the push actually reached the production branch, and check the deploy log says “Success”. Pages keeps every deployment; you can roll back to any previous one from the dashboard, which is also your emergency undo.
Custom domain stuck on “verifying”. Almost always DNS: the record is missing, proxied incorrectly, or nameservers have not moved yet. Wait an hour before changing anything — most “stuck” domains resolve themselves.
A page returns the homepage instead of a 404. Your output is missing a 404.html. Astro builds one automatically from src/pages/404.astro if that file exists; without it, Pages falls back to single-page-app behaviour and serves the index page for unknown paths, which also confuses search engines about which URLs are real.
Redirects are not firing. Confirm the file is named exactly _redirects, sits in public/ (so it lands in the root of dist), and that the path you are testing is not also a real built page — real files win over rules.
FAQ
What are the correct Cloudflare Pages build settings for Astro?
Framework preset: Astro. Build command: npm run build. Build output directory: dist. If you set those three and nothing else, a standard Astro site deploys cleanly.
Do I need a Cloudflare adapter for a static Astro site? No. A purely static site needs no adapter — Pages serves the built files directly. You only add the Cloudflare adapter when you use on-demand rendered routes, and the current adapter deploys to Workers rather than Pages.
Why is my custom domain stuck on a certificate warning? Usually DNS propagation or a conflicting record left from a previous host. Check for old A or CNAME records pointing elsewhere, then give the certificate up to a few hours before changing anything else.
How do redirects work on Cloudflare Pages?
A plain-text _redirects file in your public folder, one rule per line: old path, new path, status code. The starter file in Step 5 covers the moves a new site actually needs.
Is Cloudflare Pages free enough for a real blog? On the published free limits (checked November 2026) — 500 builds a month, 20,000 files, unlimited static bandwidth — yes, with room to spare. You would need a very large media library or a very busy monorepo to outgrow it.
What to do next
Deploy something small this week — even a one-page site — so the GitHub → Pages → domain path is muscle memory before it matters. The whole route, repo to custom domain, takes about twenty minutes once you have done it twice, and every Astro project you start afterwards inherits the settings table and the redirects file instead of reinventing them. Save your _redirects file in the repo from day one; adding redirects before you change URLs is free, while reconstructing them after is archaeology. For starting points already configured for this flow, browse the themes page, and keep the free checklists on the resources page nearby for launch day.
If you are arriving from WordPress instead, migrating WordPress to Astro sequences the move so you do not lose your URLs. And if your site outgrows purely static output, Astro on Cloudflare, the complete setup covers the adapter, bindings, and the Workers route in full.
Category: Astro Themes & Starters · Tags: #astro #cloudflare #deployment #hosting #tutorial
Next step: this article is part of Astro guides, which covers the full topic from first setup to advanced use.