logo My Digital Garden

Netlify Redirects

By James Kolean on Apr 3, 2020
Source repository: https://github.com/jameskolean/gatsby-netlify-redirects
NetlifyGatsbyDev Ops
banner

Netlify allows redirects at the edge. There is an example Gatsby project here.

You basically have two options, the first is the simplest and the second allows for complex rules:

Use _redirects

This requires a file called _redirects in the static folder. Then just add your redirects redirects. See Netlify docs here

Example

# redirect request for non-existent page at /no-where to /welcome-to-the-blog
/no-where              /welcome-to-the-blog

Or use netlify.toml

This requires a file called netlify.toml in the project root. In this file you can add redirects with rules. Here is an example of different routes based on Locales. Note: As of this writing there is a bug preventing this from working with complex accepts-language header. It should be fixed in Q2 of 2020.

[[redirects]]
  from = "/no-where/*"
  to = "/welcome-to-the-blog"
  conditions = {Language = ["en"], Country = ["US"]}

[[redirects]]
  from = "/no-where/*"
  to = "/hello-world"
  conditions = {Language = ["fr"], Country = ["FR"]}

Routing based on roles

This requires a paid plan, for details see the post here.

© Copyright 2024 Digital Garden cultivated by James Kolean.