Logo kremalicious

Redirect plugin for Markdown Pages in Gatsby

• updated

Plugin for Gatsby to create redirects based on a list in your Markdown frontmatter, mimicking the behavior of jekyll-redirect-from.

Redirect plugin for Markdown Pages in Gatsby

Features

By adding a redirect_from list of URLs to your Markdown file’s YAML frontmatter, this plugin creates client-side redirects for all of them at build time, with Gatsby’s createRedirect used under the hood.

By combining this plugin with gatsby-plugin-meta-redirect you get simple server-side redirects from your redirect_from lists out of the box. You can also combine it with any other plugin picking up Gatsby createRedirect calls to get proper SEO-friendly server-side redirects for your hosting provider.

Usage

First, install the plugin from your project’s root:

Terminal window
cd yourproject/
npm i gatsby-redirect-from gatsby-plugin-meta-redirect

Then add it to your gatsby-config.js file under plugins:

gatsby-config.js
plugins: [
'gatsby-redirect-from',
'gatsby-plugin-meta-redirect' // make sure this is always the last one
]

That’s it for the configuration.

Finally, use the key redirect_from followed by a list in your Markdown file’s YAML frontmatter:

post.md
---
title: Aperture File Types
redirect_from:
- /new-goodies-aperture-file-types-icons/
- /goodie-updated-aperture-file-types-v11/
- /aperture-file-types-v12-released/
- /2008/04/aperture-file-types/
# note: trailing slashes are required
---

Default Query

Plugin assumes the default setup from gatsby-starter-blog, with Markdown files processed by gatsby-transformer-remark, and adding a field slug for each markdown node. Resulting in the availability of a allMarkdownRemark query.

Head over to gatsby-starter-blog’s gatsby-node.js file to see how this is done, or follow the Adding Markdown Pages tutorial.

Optionally, you can pass a different query to this plugin’s configuration.

Server-Side Redirects

Gatsby’s createRedirect only creates client-side redirects, so further integration is needed to get SEO-friendly server-side redirects or make your redirects work when users have JavaScript disabled. Which is highly dependent on your hosting provider: if you want to have the proper HTML status codes like 301, Apache needs .htaccess rules for that, Nginx needs rewrite rules, S3 needs RoutingRules, Vercel needs entries in a vercel.json, Netlify needs a _redirects file, and so on.

One simple way, as suggested by default in installation, is to use gatsby-plugin-meta-redirect to generate static HTML files with a <meta http-equiv="refresh" /> tag for every createRedirect call in their <head>. So it works out of the box with this plugin without further adjustments.

This way is the most compatible way of handling redirects, working with pretty much every hosting provider. Only catch: it’s only for usability, no SEO-friendly 301 redirect is set anywhere.

For some hosting providers additional plugins are available which will pick up the redirects created by this plugin and create server-side redirects for you. Be sure to add any of those plugins after gatsby-redirect-from in your gatsby-config.js:

ProviderPlugin
Netlifygatbsy-plugin-netlify
Vercelgatsby-plugin-zeit-now
AWS S3gatsby-plugin-s3
Nginxgatsby-plugin-nginx-redirect
Apachegatsby-plugin-htaccess-redirects

Options

Plugin does not require to be configured but additional customization options are available:

OptionDefaultDescription
queryallMarkdownRemarkModify the query being used to get the frontmatter data. E.g. if you use MDX, set allMdx here.

Add options to the plugins’s configuration object in gatsby-config.js like so:

gatsby-config.js
plugins: [
{
resolve: 'gatsby-redirect-from',
options: {
query: 'allMdx'
}
},
'gatsby-plugin-meta-redirect' // make sure this is always the last one
]

Check out & contribute

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

GitHub

Changelog

sourced from kremalicious/kremalicious/gatsby-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.0.5

  • Bump release-it from 16.0.0 to 16.1.0 #389
  • dependency updates fb8d6cf

v1.0.4

8 July 2023

  • Bump eslint from 8.38.0 to 8.44.0 #387
  • package updates, remove eslint-plugin-prettier 1bec87e
  • Release 1.0.4 7703505

v1.0.3

27 April 2023

  • Bump vm2 from 3.9.16 to 3.9.17 #364
  • Bump vm2 from 3.9.15 to 3.9.16 #363
  • Release 1.0.3 26bcf8d
  • dependency security fix 3914619

v1.0.2

12 April 2023

  • Bump release-it from 15.9.1 to 15.10.0 #358
  • Bump @babel/core from 7.21.3 to 7.21.4 #359
  • Bump @babel/preset-env from 7.20.2 to 7.21.4 #357
  • Bump vm2 from 3.9.11 to 3.9.15 #360
  • dependency updates 6e7faed
  • Release 1.0.2 e704f7e

v1.0.1

23 March 2023

  • Bump eslint from 8.33.0 to 8.36.0 #350
  • Bump prettier from 2.8.3 to 2.8.4 #343
  • Bump release-it from 15.6.0 to 15.9.0 #352
  • Bump webpack from 5.75.0 to 5.76.1 #351
  • Bump @babel/core from 7.20.12 to 7.21.3 #353
  • Bump @babel/cli from 7.20.7 to 7.21.0 #346
  • Bump http-cache-semantics from 4.1.0 to 4.1.1 #342
  • Bump eslint from 8.32.0 to 8.33.0 #341
  • Bump ua-parser-js from 0.7.32 to 0.7.33 #340
  • Bump engine.io and gatsby #339
  • Bump eslint-config-prettier from 8.5.0 to 8.6.0 #338
  • Bump eslint from 8.31.0 to 8.32.0 #337
  • Bump prettier from 2.8.1 to 2.8.3 #336
  • Bump @babel/core from 7.20.5 to 7.20.12 #335
  • Bump @babel/cli from 7.19.3 to 7.20.7 #330
  • Bump release-it from 15.5.1 to 15.6.0 #333
  • Bump eslint from 8.29.0 to 8.31.0 #332
  • Bump json5 from 1.0.1 to 1.0.2 #334
  • Bump prettier from 2.8.0 to 2.8.1 #327
  • Bump chalk from 5.1.2 to 5.2.0 #328
  • Bump got and gatsby #326
  • Bump qs and express #325
  • Bump decode-uri-component from 0.2.0 to 0.2.2 #324
  • Bump @babel/core from 7.20.2 to 7.20.5 #323
  • Bump eslint from 8.27.0 to 8.29.0 #322
  • Bump release-it from 15.5.0 to 15.5.1 #321
  • Bump prettier from 2.7.1 to 2.8.0 #320
  • Bump loader-utils from 1.4.1 to 1.4.2 #318
  • Bump socket.io-parser from 4.0.4 to 4.0.5 #317
  • package updates 4a4cfa8
  • Release 1.0.1 454d0ee

v1.0.0

8 November 2022

  • Bump loader-utils from 1.4.0 to 1.4.1 #316
  • Bump eslint from 8.26.0 to 8.27.0 #315
  • Bump chalk from 5.0.1 to 5.1.2 #314
  • Bump @babel/preset-env from 7.19.4 to 7.20.2 #313
  • Bump release-it from 15.4.2 to 15.5.0 #312
  • Bump @babel/core from 7.19.6 to 7.20.2 #311
  • Bump parse-url and gatsby-telemetry #310
  • Bump eslint from 8.23.1 to 8.26.0 #308
  • Bump @babel/core from 7.19.1 to 7.19.6 #309
  • Bump @babel/preset-env from 7.19.1 to 7.19.4 #307
  • Bump @babel/cli from 7.18.10 to 7.19.3 #305
  • Bump vm2 from 3.9.9 to 3.9.11 #302
  • Bump @babel/cli from 7.18.6 to 7.18.10 #297
  • Bump @babel/preset-env from 7.18.6 to 7.19.1 #298
  • Bump release-it from 15.4.1 to 15.4.2 #299
  • Bump @babel/core from 7.18.6 to 7.19.1 #300
  • Bump ws from 7.4.5 to 7.4.6 #296
  • Bump shell-quote from 1.7.2 to 1.7.3 #295
  • Bump eslint from 8.23.0 to 8.23.1 #294
  • Bump release-it from 15.1.1 to 15.4.1 #293
  • Bump eslint from 8.15.0 to 8.23.0 #291
  • Bump file-type from 16.5.3 to 16.5.4 #285
  • Bump terser from 5.12.1 to 5.14.2 #284
  • Bump eslint-plugin-prettier from 4.0.0 to 4.2.1 #279
  • Bump prettier from 2.6.2 to 2.7.1 #281
  • Bump moment from 2.29.2 to 2.29.4 #278
  • Bump release-it from 14.14.1 to 15.1.1 #274
  • Bump @babel/cli from 7.17.6 to 7.18.6 #276
  • Bump @babel/preset-env from 7.16.11 to 7.18.6 #275
  • Bump @babel/helper-define-map from 7.16.7 to 7.18.6 #273
  • Bump @babel/core from 7.17.9 to 7.18.6 #272
  • Bump devcert from 1.2.0 to 1.2.1 #269
  • Bump eventsource from 1.1.0 to 1.1.1 #267
  • Bump parse-url from 6.0.0 to 6.0.2 #277
  • Bump sharp from 0.30.3 to 0.30.6 #268
  • Bump eslint from 8.13.0 to 8.15.0 #261
  • Release 1.0.0 112d72e
  • Gatsby v5 support b3f7933

v0.5.0

10 April 2022

  • Bump moment from 2.29.1 to 2.29.2 #254
  • Bump prettier from 2.6.1 to 2.6.2 #253
  • Bump release-it from 14.13.1 to 14.14.0 #252
  • Bump prettier from 2.6.0 to 2.6.1 #251
  • Bump eslint from 8.11.0 to 8.12.0 #250
  • Bump minimist from 1.2.5 to 1.2.6 #249
  • Bump @babel/core from 7.17.5 to 7.17.8 #247
  • Bump release-it from 14.12.5 to 14.13.1 #246
  • Bump prettier from 2.5.1 to 2.6.0 #248
  • Bump eslint from 8.10.0 to 8.11.0 #245
  • default to Gatsby v4 peer dependency e309c03
  • package updates 9419f7d
  • Create codeql-analysis.yml 46528a5

v0.4.4

11 March 2022

  • Bump release-it from 14.12.4 to 14.12.5 #242
  • Bump eslint-config-prettier from 8.4.0 to 8.5.0 #244
  • Bump url-parse from 1.5.7 to 1.5.10 #243
  • Bump eslint from 8.9.0 to 8.10.0 #241
  • Bump @babel/cli from 7.17.3 to 7.17.6 #240
  • Bump nanoid from 3.1.30 to 3.3.1 #239
  • Bump @babel/core from 7.17.2 to 7.17.5 #236
  • Bump eslint-config-prettier from 8.3.0 to 8.4.0 #237
  • Bump @babel/cli from 7.17.0 to 7.17.3 #238
  • Bump url-parse from 1.5.3 to 1.5.7 #235
  • Bump engine.io from 4.1.1 to 4.1.2 #234
  • Bump follow-redirects from 1.14.1 to 1.14.8 #233
  • Bump @babel/core from 7.17.0 to 7.17.2 #232
  • Bump eslint from 8.8.0 to 8.9.0 #231
  • Bump @babel/cli from 7.16.8 to 7.17.0 #228
  • Bump auto-changelog from 2.3.0 to 2.4.0 #229
  • Bump @babel/core from 7.16.12 to 7.17.0 #230
  • Bump eslint from 8.7.0 to 8.8.0 #227
  • Bump @babel/preset-env from 7.16.8 to 7.16.11 #224
  • Bump release-it from 14.12.3 to 14.12.4 #225
  • Bump @babel/core from 7.16.7 to 7.16.12 #226
  • Bump release-it from 14.12.1 to 14.12.3 #220
  • Bump @babel/preset-env from 7.16.7 to 7.16.8 #221
  • Bump @babel/cli from 7.16.7 to 7.16.8 #222
  • Bump eslint from 8.6.0 to 8.7.0 #223
  • Bump release-it from 14.11.8 to 14.12.1 #219
  • Bump @babel/cli from 7.16.0 to 7.16.7 #218
  • Bump eslint from 8.5.0 to 8.6.0 #217
  • Bump @babel/preset-env from 7.16.5 to 7.16.7 #216
  • Bump @babel/core from 7.16.5 to 7.16.7 #215
  • Bump @babel/helper-define-map from 7.16.5 to 7.16.7 #214
  • Bump @babel/helper-define-map from 7.16.0 to 7.16.5 #213
  • Bump @babel/preset-env from 7.16.4 to 7.16.5 #211
  • Bump @babel/core from 7.16.0 to 7.16.5 #210
  • Bump eslint from 8.4.1 to 8.5.0 #212
  • Bump eslint from 8.4.0 to 8.4.1 #209
  • Bump prettier from 2.5.0 to 2.5.1 #208
  • Bump eslint from 8.3.0 to 8.4.0 #207
  • package updates c74c7f1
  • Release 0.4.4 c9e537a

v0.4.3

28 November 2021

  • Bump eslint from 8.0.1 to 8.1.0 #199
  • Bump eslint from 8.0.0 to 8.0.1 #197
  • Bump @babel/core from 7.15.5 to 7.15.8 #195
  • Bump @babel/preset-env from 7.15.6 to 7.15.8 #194
  • Bump eslint from 7.32.0 to 8.0.0 #196
  • Bump release-it from 14.11.5 to 14.11.6 #193
  • package updates 58eddd1
  • Release 0.4.3 2e14e0e

v0.4.2

20 September 2021

  • Bump prettier from 2.4.0 to 2.4.1 #192
  • Bump @babel/cli from 7.15.4 to 7.15.7 #191
  • Bump @babel/preset-env from 7.14.4 to 7.15.6 #189
  • Bump eslint-plugin-prettier from 3.4.0 to 4.0.0 #190
  • Bump eslint from 7.28.0 to 7.32.0 #188
  • Bump prettier from 2.3.1 to 2.4.0 #187
  • Bump @babel/helper-define-map from 7.13.12 to 7.15.4 #186
  • Bump @babel/cli from 7.14.3 to 7.15.4 #185
  • Bump @babel/preset-env from 7.14.4 to 7.15.4 #184
  • Bump @babel/core from 7.14.3 to 7.15.5 #183
  • Bump release-it from 14.8.0 to 14.11.5 #182
  • Bump eslint from 7.27.0 to 7.28.0 #163
  • Bump release-it from 14.7.0 to 14.8.0 #164
  • Bump prettier from 2.3.0 to 2.3.1 #165
  • Bump release-it from 14.6.2 to 14.7.0 #161
  • Bump @babel/preset-env from 7.14.2 to 7.14.4 #162
  • Bump auto-changelog from 2.2.1 to 2.3.0 #160
  • Release 0.4.2 b5d77a4
  • Support Gatsby v4 65d1c40

v0.4.1

23 May 2021

v0.4.0

23 May 2021

  • Bump lodash from 4.17.19 to 4.17.21 #151
  • Bump @babel/eslint-parser from 7.13.10 to 7.14.2 #152
  • Bump @babel/core from 7.13.10 to 7.14.2 #153
  • Bump @babel/preset-env from 7.13.10 to 7.14.2 #154
  • Bump release-it from 14.5.0 to 14.6.2 #150
  • Bump eslint from 7.22.0 to 7.26.0 #149
  • Bump handlebars from 4.7.6 to 4.7.7 #147
  • Bump @babel/eslint-parser from 7.13.8 to 7.13.10 #127
  • Bump @babel/core from 7.13.8 to 7.13.10 #128
  • Bump @babel/cli from 7.13.0 to 7.13.10 #129
  • Bump @babel/preset-env from 7.13.8 to 7.13.10 #130
  • Bump eslint from 7.21.0 to 7.22.0 #131
  • Bump @babel/core from 7.12.16 to 7.13.8 #125
  • Bump eslint from 7.20.0 to 7.21.0 #124
  • Bump @babel/eslint-parser from 7.12.16 to 7.13.8 #123
  • Bump eslint-config-prettier from 7.2.0 to 8.1.0 #120
  • Bump @babel/cli from 7.12.16 to 7.13.0 #121
  • Bump @babel/preset-env from 7.12.16 to 7.13.8 #122
  • Bump release-it from 14.4.0 to 14.4.1 #118
  • Bump eslint from 7.19.0 to 7.20.0 #114
  • Bump @babel/core from 7.12.10 to 7.12.16 #113
  • Bump @babel/cli from 7.12.10 to 7.12.16 #111
  • Bump @babel/preset-env from 7.12.11 to 7.12.16 #112
  • Bump @babel/eslint-parser from 7.12.1 to 7.12.16 #110
  • Bump release-it from 14.2.2 to 14.4.0 #109
  • package updates e57fdbd
  • Bump release-it from 14.4.1 to 14.5.0 47d593a
  • Release 0.4.0 375d062

v0.3.0

1 February 2021

  • switch CI #103
  • Bump release-it from 14.0.3 to 14.2.0 #70
  • Bump eslint-config-prettier from 6.12.0 to 6.13.0 #68
  • Bump @babel/preset-env from 7.11.5 to 7.12.1 #67
  • Bump @babel/core from 7.11.6 to 7.12.3 #69
  • Bump @babel/cli from 7.11.6 to 7.12.1 #65
  • Bump eslint from 7.10.0 to 7.11.0 #60
  • Bump eslint-config-prettier from 6.11.0 to 6.12.0 #58
  • Bump eslint from 7.9.0 to 7.10.0 #57
  • Bump auto-changelog from 2.2.0 to 2.2.1 #56
  • Bump prettier from 2.1.1 to 2.1.2 #55
  • Bump release-it from 14.0.2 to 14.0.3 #54
  • Bump eslint from 7.8.1 to 7.9.0 #53
  • Bump node-fetch from 2.6.0 to 2.6.1 #52
  • Bump release-it from 14.0.1 to 14.0.2 #51
  • Bump release-it from 13.7.0 to 14.0.1 #50
  • Bump @babel/cli from 7.11.5 to 7.11.6 #48
  • Bump @babel/core from 7.11.5 to 7.11.6 #49
  • Bump eslint from 7.8.0 to 7.8.1 #47
  • Bump @babel/preset-env from 7.11.0 to 7.11.5 #45
  • Bump @babel/cli from 7.10.5 to 7.11.5 #44
  • Bump @babel/core from 7.11.4 to 7.11.5 #43
  • Bump eslint from 7.7.0 to 7.8.0 #46
  • Bump prettier from 2.0.5 to 2.1.1 #41
  • Bump release-it from 13.6.9 to 13.7.0 #42
  • Bump release-it from 13.6.8 to 13.6.9 #39
  • Bump @babel/core from 7.11.1 to 7.11.4 #38
  • Bump release-it from 13.6.7 to 13.6.8 #37
  • Bump release-it from 13.6.6 to 13.6.7 #35
  • Bump eslint from 7.6.0 to 7.7.0 #36
  • Bump @babel/core from 7.11.0 to 7.11.1 #34
  • Bump release-it from 13.6.5 to 13.6.6 #30
  • Bump @babel/preset-env from 7.10.4 to 7.11.0 #31
  • Bump @babel/core from 7.10.5 to 7.11.0 #32
  • Bump eslint from 7.5.0 to 7.6.0 #33
  • Bump lodash from 4.17.15 to 4.17.19 #29
  • Bump release-it from 13.6.4 to 13.6.5 #25
  • Bump @babel/core from 7.10.4 to 7.10.5 #27
  • Bump @babel/cli from 7.10.4 to 7.10.5 #26
  • Bump eslint from 7.4.0 to 7.5.0 #28
  • Bump auto-changelog from 2.1.0 to 2.2.0 #23
  • Bump eslint from 7.3.1 to 7.4.0 #22
  • Create Dependabot config file #21
  • package updates 327cf8c
  • package updates 25cbbe9
  • update lock file c3b8537

v0.2.4

23 May 2020

v0.2.3

22 May 2020

v0.2.2

17 May 2020

  • Bump release-it from 12.6.3 to 13.6.0 #20
  • Bump eslint-config-prettier from 4.3.0 to 6.11.0 #19
  • Bump eslint from 5.16.0 to 7.0.0 #18
  • Bump chalk from 2.4.2 to 4.0.0 #17
  • Bump prettier from 1.19.1 to 2.0.5 #15
  • Bump cross-env from 5.2.1 to 7.0.2 #16
  • Bump auto-changelog from 1.16.4 to 2.0.0 #14
  • fix typo on documentation #13
  • Release 0.2.2 265be4c
  • bump more packages 1341254
  • formatting changes b8322b2

v0.2.1

11 June 2019

v0.2.0

11 June 2019

  • Update docs & dependencies #12
  • specify node 8 instead of 10 as engine #11
  • Support alternative markdown queries (such as allMdx) #10
  • Update release-it to the latest version 🚀 #8
  • update docs, mention new options 07835d8
  • simplify release-it tasks, add changelog 3ec4ccf
  • add prettier 3f33f65

v0.1.1

30 August 2018

  • integrate with gatsby-plugin-meta-redirect, notes on server rendering 2cd07ee
  • Release 0.1.1 e72d5ab

v0.1.0

30 August 2018

v0.0.3

30 August 2018

v0.0.2

30 August 2018

Mastodon

Have a comment?

Hit me up @krema@mas.to

Bitcoin

Found something useful?

Say thanks with BTC or ETH

Github

Edit on GitHub

Contribute to this post