Blog / Craft

How to set up HTTP redirects on Netlify

Setting up HTTP redirects (like 301, 302) is simple with Netlify.

Netlify is Spinal’s current preferred static site hosting option. One of their features is to set up HTTP redirects.

Check this blog for a primer about what HTTP redirects are and how they can impact your SEO.

By letting Netlify handle your redirects, the request is handled before it reaches your app. Also if you run a statically rendered site, like Jekyll or Gatsby, pages are static and there is no “router” that can handle this for you.

All redirects for Netlify are handled through a plain _redirects file (notice there is no extension) that is stored in the root of your site’s folder. Each line represent one redirect rule.

For example:

/cms-for-jekyll /cms-for-jekyll/ 301

/subscribe/ /newsletter/ 301

/open/ /not-open/ 301

These entries are coming from Spinal’s own _redirects file. Try some of them and see these URLS redirect you. Notice how some of these redirects are only concerned with appending a / to the URL. While the /subscribe/ page was the old URL and is now on the [/newsletter/](/newsletter/) URL.

Rules are read top to bottom. So if you have the following _redirects:

/open/ /not-open/

/open/ /close/

Netlify only redirects /open/ to /not-open/.

When done, commit the file and push to production. Netlify automatically reads the file and applies any new rules it finds.

Note that certain Static Site Generators, like Jekyll and Bridgetown exclude file starting with an underscore.

Netlify’s redirect rules are capable of quite a bit more. From country-only rules, to certain request headers. But the ones described here are the ones mostly used by static marketing sites.

Written by July Forand


Get all Spinal content in your inbox

Every first Thursday of the month, we'll send the latest about Spinal in your inbox. From product updates, articles and a little peek behind the scene of building a SaaS in 2023.

Get all the latest every first Thursday of the month. No spam. Unsubscribe at any time.