Ronalds Vilciņš

Redirect Old URLs in Jekyll

Changing a Jekyll post’s title, date, or permalink can leave old links pointing at a 404 page. The jekyll-redirect-from plugin creates small redirect pages so those URLs still lead somewhere useful.

Add the gem to your Gemfile:

Ruby
gem "jekyll-redirect-from"

Then install the bundle:

Shell
bundle install

You can also install the gem directly:

Shell
gem install jekyll-redirect-from

Enable it in _config.yml:

YAML
plugins:
  - jekyll-redirect-from

Jekyll versions before 3.5.0 use the gems key instead of plugins. If you run Jekyll in safe mode to mimic GitHub Pages, add the plugin to the whitelist too:

YAML
whitelist:
  - jekyll-redirect-from

Give a post its old paths

Add redirect_from to the post’s front matter. It accepts several previous URLs:

YAML
title: My amazing post
redirect_from:
  - /post/123456789/
  - /post/123456789/my-amazing-post/

A trailing slash changes the generated output. This value:

YAML
redirect_from:
  - /post/123456789/my-amazing-post/

generates:

Text
/post/123456789/my-amazing-post/index.html

Without the trailing slash, the plugin generates a file without an extension or subdirectory:

Text
/post/123456789/my-amazing-post

Both pages contain an HTML refresh that points to the post’s current URL. You can also supply a single path without an array:

YAML
title: My other post
redirect_from: /post/123456798/

The plugin uses site.url and site.baseurl when it builds the target. That matters when the site lives below the domain root. On GitHub Pages, the Pages domain or a custom CNAME is used when site.url is not configured.

Redirect to another website

For an external destination, use redirect_to:

YAML
title: My project
redirect_to: https://github.com/

Redirects on collection items only work for files that Jekyll outputs as HTML, such as Markdown, Textile, and HTML documents.

Customise the redirect page

Create _layouts/redirect.html if you want to replace the plugin’s default redirect template. The layout receives two useful values:

This plugin uses an HTML refresh rather than a server-level HTTP redirect. If the host lets you configure real redirects, I would normally prefer those. On GitHub Pages, though, jekyll-redirect-from is a practical way to stop old links from quietly rotting.

Filed under and on

Last updated on

← Back to all articles