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 al…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Cassidy Williams

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 astro-redirects-example.netlify.app, 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!


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Cassidy Williams


Print Share Comment Cite Upload Translate Updates
APA

Cassidy Williams | Sciencx (2023-02-04T00:20:36+00:00) Setting up Netlify Redirects with Astro. Retrieved from https://www.scien.cx/2023/02/04/setting-up-netlify-redirects-with-astro/

MLA
" » Setting up Netlify Redirects with Astro." Cassidy Williams | Sciencx - Saturday February 4, 2023, https://www.scien.cx/2023/02/04/setting-up-netlify-redirects-with-astro/
HARVARD
Cassidy Williams | Sciencx Saturday February 4, 2023 » Setting up Netlify Redirects with Astro., viewed ,<https://www.scien.cx/2023/02/04/setting-up-netlify-redirects-with-astro/>
VANCOUVER
Cassidy Williams | Sciencx - » Setting up Netlify Redirects with Astro. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/04/setting-up-netlify-redirects-with-astro/
CHICAGO
" » Setting up Netlify Redirects with Astro." Cassidy Williams | Sciencx - Accessed . https://www.scien.cx/2023/02/04/setting-up-netlify-redirects-with-astro/
IEEE
" » Setting up Netlify Redirects with Astro." Cassidy Williams | Sciencx [Online]. Available: https://www.scien.cx/2023/02/04/setting-up-netlify-redirects-with-astro/. [Accessed: ]
rf:citation
» Setting up Netlify Redirects with Astro | Cassidy Williams | Sciencx | https://www.scien.cx/2023/02/04/setting-up-netlify-redirects-with-astro/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.