How to Use Netlify Redirects With a Jekyll Site

Last updated on January 1st, 2021

If you want to leverage Netlify redirects on your Jekyll site, there are a few extra steps to take before it can be fully functional. If you’re using a Netlify site with your own build process, using a _redirects file should be sufficient. In fact, Netlify’s guide on how to do redirects is a better resource if you don’t have a Jekyll site.

The reason that Jekyll requires special attention is because everything on your Jekyll site get’s compiled into the _site directory. The underscore in the _redirects file name can create a slight problem.

Let’s walk through everything that you need to do in order to set up Netlify and Jekyll redirects!

Leveraging Jekyll 4’s layout: none front matter option

Jekyll will ignore the underscore in the _redirects file name by default. You can easily get around this by creating a file (I called it netlify_redirects) on the root of your site. You can use the following code to get started:

layout: none
permalink: /_redirects

/path/to/old/ /path/to/new/

When Jekyll compiles, you’ll see the _redirects file in your _site directory.

Options for setting up Netlify redirects if you’re on Jekyll 3

If you’re site uses Jekyll below version 3.5, you’ll have to use a workaround. Jekyll 3.5 introduced a lot of new features that have been helpful in creating better site layouts and plugin workflow. This is old news, but it might be helpful to know if you’re on a legacy version of Jekyll!

The above recommendation to for adding redirects won’t work because layout: none will throw an error. You can alternatively create a ‘none’ layout in your _layouts directory and have that layout help you generate a blank file.

│   │   none.html

Create a static file and have Netlify create the redirects file for you

In maybe the edge of the edge cases, you might be in a situation where you can’t generate a _redirects file. In this case, you can create a regular text file on your root. Let’s say netlify_redirects.txt. From here, login to your Netlify account. Find Site Settings > Build & Deploy.

From here, look for Build Command. Use the snipplet below to help generate a _redirects file when Netlify compiles your site.

jekyll build --destination _site && cp _site/netlify_redirects.txt _site/_redirects

Netlify admin panel - Build & Deploy

First published on November 22, 2019