Cassidy Williams

Software Engineer in Chicago

Cassidy's face

Setting up Netlify Redirects with Astro

I struggled recently setting up Netlify Redirects with Astro, until I realized I needed to copy the file from the root of my directory to the built output.

Luckily, this is a very small change to make in your package.json for your project! First of all, make sure you have a _redirects file at the top level of your project.

Under your scripts, change the build command to include cp (copy) to the final built folder. I’m just using the default naming here (dist), but you can use whatever your site uses:

	"scripts": {
		// ...
		"build": "astro build && cp _redirects dist/_redirects",
		// ...

And voilà! That’s all you need to get it working!

Working example

Here’s an example repository for this. The website (just using a basic Astro template) is deployed at, and you can go to /fart and it’ll redirect you to /fish.

The _redirects file shows you a couple options (and a fallback) for how you can set it up, and here’s what the little script looks like in context!

Update after talking to the Astro team: Putting the _redirects file in the public/ directory works now for purely statically generated sites. I think if you use server-side rendering, then you still have to do a script like the above. There’s also a Build Plugin made by one of the Astro contributors, Bryce Russell, that can solve this for you!

View posts by tag

#advice #events #technical #learning #musings #work #meta #personal