Changing a blog URL seems simple until someone follows an old bookmark and reaches a 404 page. The original article may still exist under a new slug, but search engines, external websites, and readers do not automatically know where it moved.
This is where HTTP redirects and canonical URLs solve related—but different—problems.
A permanent redirect sends visitors and crawlers from an obsolete URL to its replacement. A canonical annotation tells search engines which URL you prefer when several URLs contain duplicate or very similar content. Your sitemap and internal links should reinforce that preference.
In this guide, you’ll learn to configure 301 redirects for a static Astro site on Cloudflare Pages, generate consistent canonical tags, handle the www hostname, and test a URL change before publishing. The examples use illustrative routes and assume a Git-based Astro website; adapt them to your own project before deployment.
1. Why URL Changes Can Break a Healthy Website
A published URL is more than an address in the browser. It may be referenced in search results, social posts, newsletters, other articles, analytics reports, and external websites.
Common reasons URLs change include:
- An editor updates a blog post slug after publication.
- A category or directory structure changes.
- Two overlapping tutorials are consolidated into one.
- A site chooses a single
wwwor non-wwwhostname. - Trailing-slash rules are made consistent.
- A domain migration introduces a new host.
Without a deliberate migration plan, a new article can be published successfully while the old address becomes an unhelpful 404. A redirect preserves a usable path to the replacement. It does not, by itself, guarantee that Google will transfer every ranking signal or immediately index the new URL.
2. Understand 301, 308, 302, and Canonical Tags
These mechanisms are not interchangeable.
| Mechanism | Purpose | Typical use |
|---|---|---|
301 Moved Permanently |
Permanent HTTP redirect | Published post moved to a new slug |
308 Permanent Redirect |
Permanent redirect that preserves HTTP method | Permanent endpoint migration when method preservation matters |
302 Found |
Temporary HTTP redirect | Short-lived campaign or maintenance change |
rel="canonical" |
Preferred representative URL hint | Similar content remains reachable at multiple URLs |
| XML sitemap | Lists URLs you want search engines to discover | Published canonical pages |
For a standard published article that permanently changes location, a server-side 301 is generally appropriate. A canonical tag does not navigate the visitor away from an old URL; the original page remains accessible.
Google treats permanent redirects and canonical annotations as strong signals, while sitemap inclusion is a weaker canonicalization signal. The signals should agree instead of pointing to competing destinations.
Official references: Google’s redirects guide and canonicalization guidance.
3. Map Old URLs to Their Real Replacements
Before writing configuration, make a small migration table.
| Old path (example only) | Replacement path | Action |
|---|---|---|
/blog/old-astro-guide/ |
/blog/updated-astro-guide/ |
301 redirect |
/tutorials/legacy-setup/ |
/tutorials/current-setup/ |
301 redirect |
/blog/retired-offer/ |
No comparable replacement | Return an appropriate 404 or 410 |
The example paths above are not existing pages on this website. They are templates for your own migration plan.
Choose the closest relevant replacement for each old page. Avoid sending every deleted article to your homepage. Google warns that irrelevant mass redirects can confuse users and be treated as soft 404 errors.
Also record whether the current URL has backlinks, internal references, search traffic, or an entry in the sitemap. This will help you prioritize important migrations.
4. Configure 301 Redirects with Cloudflare Pages _redirects
Cloudflare Pages supports a plain-text _redirects file for requests served as static assets. In a typical Astro project, place it here:
public/_redirects
Files in Astro’s public/ directory are copied to the build output without transformation. For a standard build, the resulting file should be present at dist/_redirects.
Add only the routes you actually need:
# SOURCE DESTINATION STATUS
/blog/old-astro-guide/ /blog/updated-astro-guide/ 301
/tutorials/legacy-setup/ /tutorials/current-setup/ 301
Each active rule must contain the source, destination, and status on a single line. Comments begin with #.
Important: These source and destination paths are illustrative. Do not copy them into your live _redirects file unless you have created the matching pages and genuinely need the redirects.
If a new post already exists at /blog/updated-astro-guide/, visiting /blog/old-astro-guide/ should produce an HTTP 301 with a Location header pointing to the new path. The destination should then return a successful page response.
Cloudflare notes that static rules take precedence in matching order, with static redirects before dynamic redirects. The file also has rule-count and line-length limits; check the current limits before generating large lists automatically.
Critical limitation: _redirects rules are not applied to responses served by Cloudflare Pages Functions. If a Function handles the path you are migrating, implement the redirect in the appropriate Function or configure a suitable Cloudflare redirect rule at the edge. Confirm the routing architecture before assuming the static file covers every request.
Official reference: Cloudflare Pages redirects.

Figure 1: Cloudflare Pages can apply static _redirects rules before serving the destination page. Requests handled by Pages Functions require separate consideration.
5. Build and Check the Redirect File
Before publishing, build the project from a clean working branch:
npm ci
npm run build
Confirm that your redirect file reached the output folder:
# macOS / Linux
ls -l dist/_redirects
On Windows PowerShell:
Test-Path .\dist\_redirects
Get-Content .\dist\_redirects
This checks file placement and syntax visibility, not the deployed redirect behavior. The Astro development server or static preview is not a substitute for testing how Cloudflare Pages interprets _redirects.
Use a Cloudflare Pages preview deployment to verify the actual HTTP response before merging the change.
6. Why Astro’s Own Redirect Configuration Is Different
Astro also supports a redirects option in astro.config.mjs.
For example:
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://example.com',
redirects: {
'/old-page': '/new-page',
},
});
However, an Astro application rendered as a fully static site without a suitable adapter can generate client-side HTML redirects, not necessarily true HTTP 301 responses. This is particularly important for SEO migrations where you intend to return a server-side redirect status.
Do not assume a redirect option inside Astro produces the same HTTP behavior as Cloudflare Pages _redirects.
When deciding which mechanism to use, check the output mode, adapter, hosting configuration, and whether the route is handled statically or by a Function. Verify the final result with an actual HTTP request.
Official reference: Astro configuration reference.
7. Configure the Preferred Production Domain
A site may be reachable through several versions of its hostname:
http://example.com/
https://example.com/
https://www.example.com/
Choose one preferred public origin. For the examples below, we’ll use the non-www HTTPS variant:
https://example.com/
In astro.config.mjs, configure the site value and your existing trailing-slash policy:
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://example.com',
trailingSlash: 'always',
});
This is a minimal example, not a replacement for your existing config. Preserve all current integrations, adapters, image settings, build options, and plugins. If your project already has a site or trailingSlash value, audit it rather than adding duplicate configuration.
Astro uses the configured site origin for features such as sitemap generation and canonical URL construction.
The hosting platform can still influence how prerendered routes behave with or without a trailing slash. Confirm both URL forms in the real deployment.
8. Create a Self-Referencing Canonical URL in Astro
The preferred URL should appear in the HTML <head> of the final page:
<link
rel="canonical"
href="https://example.com/blog/updated-astro-guide/"
/>
Google recommends absolute canonical URLs. Published canonical pages should generally reference themselves. Avoid query parameters used only for tracking when generating a canonical URL.
For an Astro blog with an established trailing-slash convention, a reusable component might look like this:
src/components/CanonicalLink.astro
---
if (!Astro.site) {
throw new Error('Set site in astro.config.mjs');
}
// For content pages whose URLs always end in "/".
const pathname = Astro.url.pathname;
const normalizedPath = pathname === '/'
? '/'
: `${pathname.replace(/\/+$/, '')}/`;
// Excludes query strings and hash fragments.
const canonicalUrl = new URL(
normalizedPath,
Astro.site,
).toString();
---
<link rel="canonical" href={canonicalUrl} />
Then include it once in the shared page layout’s <head>:
---
import CanonicalLink from '../components/CanonicalLink.astro';
---
<html lang="en">
<head>
<meta charset="utf-8" />
<CanonicalLink />
</head>
<body>
<slot />
</body>
</html>
The layout above is simplified for illustration. Do not copy over your existing production layout. If your SEO component already emits a canonical tag, update that existing component rather than adding a second tag.
This normalization assumes all affected content routes end with a slash. It should not be applied unchanged to file-like routes such as /feed.xml or to sites intentionally using extension-based URLs. Use the convention that matches your deployed routes.
If a page needs a deliberate canonical pointing elsewhere, implement that as an explicit, reviewed override. A page with genuinely different content should not automatically point to an unrelated article.

Figure 2: Redirect and canonical signals should agree on one preferred HTTPS URL. Tracking parameters should not create competing canonical pages.
9. Redirect www to the Apex Domain at the Cloudflare Edge
A hostname redirect is different from a path redirect in the Pages _redirects file.
Cloudflare does not support domain-level redirects using Pages _redirects. Instead, use Single Redirects (or another supported edge-level redirect product).
For a www-to-apex migration, the intended behavior is:
https://www.example.com/blog/guide/
301
https://example.com/blog/guide/
In the Cloudflare dashboard, create a redirect rule matching requests to your www hostname and set the destination to the corresponding path on the apex hostname. Preserve the query string when appropriate.
Before enabling the rule, verify:
- The source hostname is configured in Cloudflare DNS and requests reach the Cloudflare proxy.
- The required TLS certificate and HTTPS handling are working.
- The rule does not accidentally match the destination hostname.
- The original path is preserved, and no redirect loop is created.
- Other existing Workers, Pages custom domains, or redirect rules do not conflict.
An example Cloudflare rule and screenshots may depend on which Rules interface is available in your account. Follow the official redirect www to root guide for current configuration options.
Do not change DNS or add a second global redirect merely because a tutorial shows one. If your production host already redirects correctly, leave that working configuration in place.
10. Keep Canonical, Internal Links, Sitemap, and Schema Aligned
A common SEO mistake is fixing one signal but forgetting the others.
After moving an article, all of the following should refer to the new canonical URL:
| Signal | Preferred behavior |
|---|---|
| Old URL | HTTP 301 to new URL |
| New URL | HTTP 200 and self-referencing canonical |
| Navigation and related articles | Link directly to new URL |
| XML sitemap | Include new URL, not the obsolete one |
BlogPosting JSON-LD |
Use new canonical URL for page identity |
BreadcrumbList |
Reflect the new navigation destination |
| Social sharing metadata | Use the correct published page URL |
For example, do not keep old-astro-guide in your Continue Reading links after the content has moved. Redirects preserve external access, but your own internal navigation should point directly to the final destination.
If you already use an Astro SEO component and an @astrojs/sitemap integration, review them before adding another implementation. Duplicate or contradictory canonical tags are worse than having one consistent, correctly configured source of truth.
11. Test an HTTP Redirect on Windows and macOS
For a real migration, replace the URL examples with the actual old and new URLs on your preview or production deployment.
On Windows PowerShell or Command Prompt, run:
curl.exe -I https://example.com/blog/old-astro-guide/
A successful permanent redirect should resemble:
HTTP/2 301
location: https://example.com/blog/updated-astro-guide/
Then inspect the destination:
curl.exe -I https://example.com/blog/updated-astro-guide/
You typically expect a successful page response such as 200 OK, along with a single canonical tag in the delivered HTML.
To inspect the redirect chain:
curl.exe -IL https://example.com/blog/old-astro-guide/
A few caveats:
- A
HEADrequest may behave differently fromGETon certain applications or proxies. CheckGETif the endpoint does not supportHEADcorrectly. - A
200response after-Ldoes not prove the first request returned301; inspect every hop. - The source path must not redirect back to itself or create a loop.
- The destination should not redirect through unnecessary intermediate URLs.
Use your browser’s Network panel to confirm the behavior as an additional check.
12. Add a Repeatable Redirect Check to Your Workflow
For a blog with frequent slug changes, maintain a list of migration mappings alongside the source code.
redirect-checks.example.json
[
{
"from": "/blog/old-astro-guide/",
"to": "/blog/updated-astro-guide/"
},
{
"from": "/tutorials/legacy-setup/",
"to": "/tutorials/current-setup/"
}
]
The file is a template. Replace the examples with migrations that genuinely exist before executing checks against a live website.
A small Node.js script can check that each source returns a permanent redirect and that the Location header matches the expected destination. A version of this script is included in the downloadable article package.
Execute it with Node.js 20 or newer:
node scripts/check-redirects.mjs \
https://your-preview-host.pages.dev \
redirect-checks.example.json
The script deliberately does not modify your website. It uses fetch() with redirect: 'manual' to inspect the original response rather than silently following the redirect.
If you add this to GitHub Actions, run it after a Cloudflare Pages preview deployment is available. A local Astro build alone cannot prove how Cloudflare will serve its redirect rules.
13. Use a Safe Release Checklist for URL Migrations
Before changing a published URL, verify the following:
- The replacement article exists and is useful to the same readers.
- The old and new URLs are recorded in a migration table.
- The redirect uses a permanent status for a permanent move.
- The redirect destination is the final URL, not an intermediate hop.
- There are no loops between hostname and path rules.
- The replacement page returns a successful response.
- The replacement page has one correct canonical tag.
- Related posts and navigation link directly to the new URL.
- The sitemap and JSON-LD contain the intended canonical URL.
- The rule has been tested on an actual Pages preview deployment.
- No CMS editorial workflow, OAuth route, or Pages Function has been broken.
- The migration is monitored after release.
Do not overwrite a working astro.config.mjs, _headers, _redirects, or CMS configuration without first comparing the current contents and verifying how the production site is routed.

Figure 3: Verify source mapping, real HTTP status codes, canonical consistency, and Search Console signals before considering a URL migration complete.
14. Monitor the Move in Google Search Console
After deployment, inspect the old and new URLs with Google Search Console’s URL Inspection tool. The old URL should redirect to the intended new page; the new page should be accessible and eligible for indexing when appropriate.
Check:
- Whether Google-selected canonical agrees with your declared canonical.
- Whether the new page is included in the current XML sitemap.
- Whether internal links still point to obsolete URLs.
- Whether server logs or analytics reveal repeated requests to unexpected paths.
- Whether indexing and search traffic change during the migration.
Indexing may take time. A correct 301 does not guarantee immediate updates in search results, and temporary fluctuations are possible.
Google recommends keeping redirects in place for as long as possible, generally at least one year for site moves, and longer when old bookmarks or inbound links may still be used. Do not remove mappings immediately after the new page appears in Search.
Official reference: Google’s site move guidance.
15. Common Redirect and Canonical Mistakes
Mistake: Assuming every Astro redirect returns HTTP 301
Static builds can create client-side redirects depending on configuration and adapter. Verify the response status served by your host.
Mistake: Adding _redirects without checking Pages Functions
A Pages Function serving a matching request does not automatically inherit _redirects. Check which system processes the request.
Mistake: Using 301 for temporary routing changes
A permanent redirect communicates a long-term destination. Use an appropriate temporary response when the move is genuinely temporary.
Mistake: Sending dozens of unrelated old pages to the homepage
If no meaningful replacement exists, serve an appropriate missing-page response. Do not force unrelated users into a page that does not answer their original intent.
Mistake: Leaving multiple competing canonical elements
A shared SEO component, page layout and individual article can accidentally output more than one canonical tag. Choose one implementation and check the final HTML.
Mistake: Forgetting absolute internal URLs
A migration may update relative links but leave absolute references pointing to old paths. Check both forms, including links inside JSON-LD and metadata.
Mistake: Redirecting www in two different systems
Overlapping Cloudflare Redirect Rules, Pages rules and application logic can cause chains or loops. Keep one clear redirect policy and test it end-to-end.
16. Frequently Asked Questions
Does a canonical tag replace a 301 redirect?
No. A canonical tag is a preference signal for search engines. A 301 moves the visitor from the old URL to a new URL. When a published article permanently moves, a server-side redirect is generally more appropriate.
Does Cloudflare Pages support 301 and 308 redirects?
Yes, _redirects supports multiple HTTP redirect status codes for applicable static responses. Confirm support and current limits in the official Cloudflare documentation.
Can I redirect www to the root domain using _redirects?
Not as a domain-level rule. Use Cloudflare Single Redirects or another suitable edge-level redirect mechanism for host normalization.
Will a 301 guarantee I keep my Google ranking?
No. A permanent redirect provides a strong migration signal, but search visibility also depends on relevance, content, indexing and other factors. Avoid claiming guaranteed ranking preservation.
Should I keep old URLs in the sitemap?
Generally, list your preferred canonical destination URLs rather than obsolete URLs that now redirect. Use consistent links and canonical annotations.
What if the new article is still a Decap CMS draft?
Do not redirect readers to an unpublished destination. Publish and verify the replacement page first, then enable the redirect according to your editorial workflow.
How do I check redirects without breaking the live site?
Use a branch and a Cloudflare Pages preview deployment. Test the HTTP response and destination before merging. The test script included in this guide is read-only.
17. Final Thoughts
Broken links are easy to spot after a migration, but a better process prevents them before visitors encounter the problem.
For a static Astro website on Cloudflare Pages, the essential workflow is straightforward: map old URLs to relevant replacements, add appropriate redirects for static routes, choose one canonical production URL, keep internal links and structured data consistent, and verify the real deployed HTTP behavior.
The most important principle is one clear destination supported by consistent signals.
That is more valuable than collecting SEO checks that disagree with each other—or adding redirect rules to a production website without testing their actual effect.
Continue Reading
- How to Find and Fix Broken Links in Astro with GitHub Actions (2026 Guide) — Automate detection of broken internal links and missing assets.
- How to Add Structured Data to Astro: BlogPosting, Breadcrumbs & JSON-LD (2026 Guide) — Align JSON-LD article identity and breadcrumbs with canonical URLs.
- Astro SEO Guide 2026: 12 Essential Steps to Optimize Your Website for Google — Review the wider technical SEO foundation.
Official References
- Cloudflare Pages: Redirects
- Cloudflare: Redirect
wwwto root - Cloudflare: Redirect options
- Astro: Configuration Reference
- Google: How to Specify Canonical URLs
- Google: Redirects and Search
- Google: Site Moves and Migrations
Technical disclosure: The sample paths and scripts are for illustration. This article does not claim that a URL migration has been performed, tested on the author’s production website, or shown to improve search rankings. Always verify your project’s existing routing and Cloudflare deployment before applying configuration.






