> 🎯 Set redirect urls in your frontmatter within your [Astro](https://astro.build) site's Markdown files. Mimics the behavior of [jekyll-redirect-from](https://github.com/jekyll/jekyll-redirect-from) and [gatsby-redirect-from](https://kremalicious.com/gatsby-redirect-from/).
>
> https://kremalicious.com/astro-redirect-from/
---
Allows you to manage redirects directly from your Markdown files' frontmatter.
Imagine you've just revamped your blog, and some of your old URLs have changed. You don't want to lose the SEO juice, nor do you want to leave your readers with broken links. This is where redirects come into play. But managing redirects can be cumbersome, especially if you have to do it manually or through server configurations.
This plugin automates this process by reading the `redirect_from` field in the frontmatter of your Markdown files and updating Astro's configuration to handle these redirects automatically.
## How it Works
By adding a `redirect_from` list in your Markdown frontmatter, the plugin integrates these redirects into Astro's [`redirects`](https://docs.astro.build/en/reference/configuration-reference/#redirects) configuration automatically, whether you're running the development server or building your project.
The plugin operates during the [`astro:config:setup`](https://docs.astro.build/en/reference/integrations-reference/#astroconfigsetup) lifecycle hook. It scans all Markdown files for the `redirect_from` key and updates Astro's configuration using [`updateConfig()`](https://docs.astro.build/en/reference/integrations-reference/#updateconfig-option). This ensures that any existing redirects are merged with new ones generated by the plugin.
Astro takes over from there, handling the redirects based on your site's build mode and in combination with any other integrations you might be using:
> For statically-generated sites with no adapter installed, this will produce a client redirect using a `<meta http-equiv="refresh">` tag and does not support status codes.
> When using SSR or with a static adapter in `output: static` mode, status codes are supported. Astro will serve redirected `GET` requests with a status of `301` and use a status of `308` for any other request method.
The plugin is designed to work with various Astro hosting integrations, most of them generate further redirect files in the places they require so this plugin works in combination with them:
Because Astro integrations are run in the order they are defined in the `integrations` array, this plugin should come before any other integrations which make use of the `redirects` config.
The plugin is designed to work without configuration, especially if your project aligns with Astro's default settings.
- Requires Astro v3 (v2 probably works too, but is not tested)
- Markdown files should be in a directory (default is `src/pages/`)
- Slugs are extracted either from the frontmatter or the file/folder path
## Installation
```bash
cd yourproject/
# Using NPM
npx astro add astro-redirect-from
# Using Yarn
yarn astro add astro-redirect-from
# Using PNPM
pnpm astro add astro-redirect-from
```
If installing manually:
```bash
npm i astro-redirect-from
```
Then load the plugin in your Astro config file:
```js title="astro.config.mjs"
import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'
export default defineConfig({
// ...
integrations: [
// make sure this is listed before any
// hosting integration
redirectFrom()
]
// ...
)}
```
That's it. Your redirects will be automatically added the next time you run `astro dev` or `astro build`. If any of them have a `redirect_from` field, that is.
[See Usage](#usage)
### Options
Options are all optional and can be passed in Astro's config file:
```js title="astro.config.mjs"
import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'
import { getMySlug } from './your-slug-function'
export default defineConfig({
// ...
integrations: [
redirectFrom({
contentDir: './content',
getSlug: getMySlug
})
]
// ...
)}
```
#### `contentDir: string`
_Default: `src/pages/`_
Specify a different directory for your Markdown files, relative to the project root.
#### `getSlug: (filePath: string) => string`
_Default: `getSlugFromFilePath()`, see below_
If you need a custom slug structure, pass a function to construct your slug from the file path.
If you use a `slug` field in your frontmatter, this will be preferred by the plugin and passing any `getSlug` function will have no effect in that case.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---
Made with ♥ by [Matthias Kretschmann](https://matthiaskretschmann.com) ([@kremalicious](https://github.com/kremalicious))