Redirects and rewrites in Netlify using Astro
A guide to understanding and using Netlify Redirects with sites built with Astro
May 9, 2025
This is a simple guide to understand how to use redirects and rewrites in Astro sites built with Netlify.
Well, it’s actually Netlify configurations in general and it should work with any framework.
So, I’ve recently changed my blog URLs from /posts/** to /blog/** and I needed to update all the links to the new URLs.
Very simple, according to Netlify, we need to have a _redirects file in the root (published) directory of our project, let’s call it dist as in 99% of the default build output directories are (tbh I’ve no idea where I got that percentage from, what a confidence :D).
let’s create the _redirects file in our root not yet published directory, and we simply copy it to the dist directory after building the project.
# _redirects
/posts/:id /blog/:id
/with-status /with-status-code 200
then in your package.json file, you need to add a new script to copy the _redirects file to the dist directory.
"scripts": {
"build": "astro build && cp _redirects dist/_redirects"
}
now, all my https://helloadel.com/posts/:id URLs will be redirected to the new https://helloadel.com/blog/:id URLs.
and the with-status URL will be redirected to the with-status-code URL with a status code of 200. which means an internal redirect, so URL will not changed and that’s what I did not want in my example, but just to show you how to do it.
This is a simple example for sites that doesn’t have a whole netlify.toml file, but if you do, you can do the same thing in the netlify.toml file using the Netlify link above for more details.