Redirect URL With GatsbyJS and Netlify

Bulk URL redirecting that resembles the .htaccess method used for Apache.

Installing the plugin

Firstly you need to install and include the plugin.

npm install --save gatsby-plugin-netlify

Include the plugin in your gatsby-config.js

plugins: [`gatsby-plugin-netlify`]Code language: JavaScript (javascript)

Creating the redirects

Netlify reads a file called “_redirects” in the project root folder. The problem however is that placing the file in the root of the Gatsby application doesn’t work, so instead we place it in the /static folder. From here the file will be copied to the base Netlify folder.

Adding redirects:

// FILE: _redirects

/some-old-url /the-new-url
/another-old-url /the-new-urlCode language: JavaScript (javascript)

Notes:

Since this is supported by Netlify, it’s not going to work in your development environment.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.