Redirect Plugin for Markdown Pages in Astro

• updated

Integration plugin for Astro to create redirects based on a list in your Markdown frontmatter, mimicking the behavior of jekyll-redirect-from and gatsby-redirect-from.

Redirect Plugin for Markdown Pages in Astro

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 and deployment tools configurations.

This plugin automates this process allowing you to add a redirect_from list in the frontmatter of your Markdown files and the plugin will update 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 configuration automatically.

The plugin operates during the astro:config:setup lifecycle hook. It scans all Markdown files for the redirect_from key and updates Astro’s configuration using updateConfig(). This ensures that any existing redirects in astro.config.mjs are merged with the 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.
Astro Configuration Reference: redirects

This plugin works 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:

ProviderPlugin
Netlify@astrojs/netlify
Vercel@astrojs/vercel
Cloudflare@astrojs/cloudflare

Prerequisites

The plugin is designed to work without configuration, especially if your project aligns with Astro’s default settings.

  • 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

Terminal window
cd yourproject/
npx astro add astro-redirect-from

If installing manually:

Terminal window
npm i astro-redirect-from

Then load the plugin in your Astro config file, making sure this plugin comes before any other integrations which make use of the redirects config:

astro.config.mjs
import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'
export default defineConfig({
// ...
integrations: [
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.

For easy debugging, a redirect_from.json is written out into Astro’s cacheDir setting, which by default is under node_modules/.astro.

See Usage

Options

All options are optional and can be passed in Astro’s config file:

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. The file path should be relative to the content directory.

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.

The default function is a great starting point:

function getSlugFromFilePath(filePath: string) {
const parsedPath = path.parse(filePath)
let slug
// construct slug as full path from either:
// - folder name if file name is index.md, or
// - file name
if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') {
slug = parsedPath.dir
} else {
slug = `${parsedPath.dir}/${parsedPath.name}`
}
return slug
}

Usage

In your Markdown file’s frontmatter, use the key redirect_from followed by a list.

---
redirect_from:
- /old-url-1
- /old-url-2
- /old-url-3.html
---

Alternatively, you can use a string instead of a list for a single redirect:

redirect_from: /old-url

Check out & contribute

Head over to GitHub to take a peek into the code, or to report some bugs.

GitHub

Changelog

sourced from kremalicious/astro-redirect-from:CHANGELOG.md

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

v1.3.4

  • chore: upgrade deps #323

v1.4.0-beta.1

3 July 2025

v1.4.0-beta.0

3 July 2025

  • add tests 7b68b65
  • add base path support and refactor path handling 6a74260
  • Release 1.4.0-beta.0 0e6d127

v1.3.3

1 May 2025

v1.3.2

1 May 2025

v1.3.1

1 April 2025

v1.3.0

19 December 2024

  • Dependency updates #197
  • Convert URL.pathname to proper File Path #196
  • Release 1.3.0 38eef0e

v1.2.0

12 August 2024

  • Warn on parsing failures; support redirect_from: url #157
  • dependency updates 53cc262
  • Release 1.2.0 dadd12d
  • formatting fixes d8bd20f

v1.1.0

26 July 2024

v1.0.8

7 June 2024

v1.0.7

8 April 2024

  • dependency updates, require Node.js v18.18.x ba876b3
  • Release 1.0.7 09212e6

v1.0.6

26 February 2024

  • Bump astro from 4.0.3 to 4.2.1 #74
  • Bump vitest from 1.0.4 to 1.2.1 #73
  • Bump vite from 5.0.8 to 5.0.12 #72
  • dependency updates 01d6266
  • Release 1.0.6 96f3f12

v1.0.5

12 December 2023

  • Bump vite from 4.5.0 to 5.0.7 #52
  • Bump astro from 3.5.5 to 4.0.3 #55
  • Bump zod and astro #40
  • Bump @types/node from 20.8.8 to 20.9.2 #39
  • Bump astro from 3.3.4 to 3.5.5 #38
  • Bump eslint from 8.52.0 to 8.54.0 #37
  • Bump @typescript-eslint/eslint-plugin from 6.9.0 to 6.11.0 #36
  • Bump release-it from 16.2.1 to 17.0.0 #34
  • bump vitest to v1.0, nested test fix d41701a
  • Release 1.0.5 f6fa4e5

v1.0.4

25 October 2023

  • Bump @typescript-eslint/eslint-plugin from 6.7.4 to 6.9.0 #25
  • Bump eslint from 8.50.0 to 8.52.0 #20
  • Bump vite from 4.4.10 to 4.5.0 #23
  • Bump @babel/traverse from 7.22.20 to 7.23.2 #18
  • Bump astro from 3.2.2 to 3.3.2 #21
  • add husky c6e24a6
  • readme updates 0c0e39a
  • add funding to package.json acdfc1e

v1.0.3

3 October 2023

  • Bump astro from 3.1.2 to 3.2.0 #6
  • Bump @types/node from 20.6.3 to 20.6.5 #1
  • Bump get-func-name from 2.0.0 to 2.0.2 #4
  • Bump eslint from 8.49.0 to 8.50.0 #3
  • Bump release-it from 16.1.5 to 16.2.0 #2
  • package updates 4024b6b
  • Release 1.0.3 91047d4
  • reduce install commands 8b9d845

v1.0.2

24 September 2023

v1.0.1

24 September 2023

v1.0.0

24 September 2023

  • test setup, unit tests for all utils 4d33283
  • refactor for better testability 09dfa97
  • getRedirects splitup, more tests 3d921d9

v0.2.4

23 September 2023

v0.2.3

23 September 2023

v0.2.2

23 September 2023

v0.2.1

23 September 2023

v0.2.0

23 September 2023

Have a comment?

Hit me up @krema@mas.to

Found something useful?

Send some tips

Edit on GitHub

Contribute to this post