Announcing the Astro Cloudflare Redirects integration
A plugin for Astro to automatically generate redirects using Cloudflare's _redirects file specification.
Cloudflare, as part of the Cloudflare Pages platform, has a file specification for redirects, usually defined via a _redirects
file. This feature allows you to specify a list of URLs and their destinations, and Cloudflare will automatically redirect traffic to those URLs to the specified destinations.
It looks like this:
This is a helpful way to keep broken URLs from piling up on your site, and I wanted to use it inside of my Astro-based site.
Luckily, the vite-plugin-cloudflare-redirect
was created by a member of Astro’s community. I used that Vite plugin and wrapped in an Astro integration package, meaning it’s incredibly easy to install:
This will do a guided install to add the integration to your Astro project.
You can also install it manually:
Once installed, you can add the integration to your Astro config:
Usage
You can use it by generating a file in public/_redirects
with the same format as the Cloudflare redirects file. For instance, if you wanted to redirect /old-url
to /new-url
, you could create a _redirects
file like this:
You can also specify a custom file location, if you’d like:
Why build this?
I like Cloudflare’s _redirects file format, but by pulling it into Astro via a Vite plugin, you can use it in any environment. Regardless of how you’re deploying your Astro application, you can use this plugin for easy redirect handling.