How to Fix Astro Redirect Settings When They Don’t Work [Static Site SEO]
The other day, I was tinkering with the article-related system of my Astro (SSG) site and mistakenly published an article with a URL different from what I intended.
It didn’t become a major problem, but one article got registered in Google Search Console with that URL…
At first, I thought time would solve it and tried to handle it by just reverting the URL, but apparently that’s nonsensical from an SEO perspective… reluctantly I decided to set up redirects.
When I tried to implement redirects in Astro, I stumbled, so I’m summarizing that here.
Target audience: Those building static sites with Astro and struggling with redirect settings / Those hosting on Cloudflare or Vercel
Why Redirects Are Important for SEO
- Avoiding duplicate content: When incorrect and correct URLs coexist, evaluation gets dispersed!!
- Link equity inheritance: With 301/308 redirects, you can transfer backlink evaluation to the new URL!!
- Crawl efficiency optimization: Reduce unnecessary 404s and save crawl budget!!
TIPGoogle distinguishes between “permanent (301/308)” and “temporary (302)” redirects to transfer evaluation. Choose the status code according to your purpose!
Prerequisites
- Astro v4 (SSG mode)
- Cloudflare Pages hosting (confirmed to work similarly on Vercel)
Method ①: Define redirects in astro.config.*
This method adds redirects
to the Astro configuration file. It’s officially recommended and easy to manage (important), so it’s sufficient for a small number of redirects!!
export default defineConfig({
redirects: {
// ① Path movement within the same site (default 301)
"/posts/old-slug": "/posts/new-slug",
// ② Dynamic routes are also OK (parameter names must match)
"/blog/[...slug]": "/articles/[...slug]",
// ③ When you want to specify status code explicitly
"/note": { // 308 = permanent POST allowed
status: 308,
destination: "/posts/note"
},
// ④ To external URL
"/docs": "https://example.com/docs"
}
});
- Advantages: Complete within Astro. HTML with
<meta http-equiv="refresh">
is automatically generated during SSG build. - Disadvantages: Writing many rules bloats the configuration file.
Method ②: Using vercel.json
(Also available on Cloudflare Pages)
If you need more detailed conditional branching or hostname-based routing, vercel.json
is convenient.
{
"redirects": [
{
"source": "/posts/old-slug",
"destination": "/posts/new-slug",
"permanent": true // 308
},
{
"source": "/blog/:year/:month/:slug",
"destination": "/posts/:slug",
"statusCode": 301
},
{
"source": "/docs/:path*",
"destination": "https://example.com/docs/:path*",
"permanent": true
},
{
"source": "/:path*",
"has": [
{ "type": "host", "value": "old.example.com" }
],
"destination": "/legacy/:path*",
"statusCode": 302
}
]
}
- Advantages
- Advanced features like wildcards and host conditions.
- Configuration files can be separated for easier maintenance.
- Disadvantages
- Depends on
trailingSlash
settings, requiring attention to trailing slash forced removal behavior.
- Depends on
WARNINGRedirects defined in
vercel.json
remove trailing slashes by default. If you’ve specifiedtrailingSlash: "always"
, consistency verification is required!
Which Should You Choose?
Case | Recommended Method |
---|---|
Small number (~10 or so) of static redirects | Method ① Astro configuration |
Dynamic paths, subdomain conditions, bulk registration | Method ② vercel.json |
For my site, since I always add trailing slashes, I adopted Method ①.
Summary
- Choose the correct status code to transfer evaluation without loss.
- Astro’s standard
redirects
is the most convenient. - For advanced conditional branching or bulk settings,
vercel.json
is useful. However, pay attention to compatibility withtrailingSlash
.
To prevent incorrect URLs from being indexed, don’t forget pre-publication checks and redirect settings!!!
Here’s how to change trailingSlash settings in Astro!!
