Setting up Netlify Redirects with Astro

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 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!

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
enable-fast-pass-development-with-google-wallet-demo-mode

Enable fast pass development with Google Wallet demo mode

Next Post
build-your-first-port-scanner-using-python

Build Your First Port Scanner using Python

Related Posts