Jekyll layout to create URL redirects with HTML and JavaScript. No plugin necessary.
Generates Javascript redirects with fall back to meta refresh redirects. Fully compatible with Github Pages.
View a live demo running on Github Pages. The code and configuration for the demo is in the gh-pages branch.
Add _layouts/redirect.html
to the _layouts
folder.
Create a _redirects
folder in the Jekyll root folder.
Add the following to _config.yml
. This adds the redirects
collection to Jekyll and sets the default layout for the collection to _layouts/redirect.html
:
collections:
redirects:
output: true
defaults:
-
scope:
type: redirects
values:
layout: redirect
A redirect is a Jekyll page which redirects the browser to a new location. Redirect files are added to the _redirects
folder and use the redirect layout by default.
Configure each redirect with front-matter variables:
permalink
(required)
The permalink is the URL of the redirect. For example:
permalink: /redirect-this-url/
destination
(required)
The destination is the URL to which the browser will be redirected. For example:
destination: /to-this-location/
canonical
(optional. defaults to false)
Set canonical
to true
to indicate the destination is the canonical URL for this redirect.
When using a sitemap generator, it is common to exclude all redirects from the sitemap. Usually this is done by adding sitemap: false
as a front-matter variable.
There are more examples located in the _redirects
folder.
An example internal canonical redirect:
---
permalink: /redirect-this-url/
destination: /to-this-location/
canonical: true
---
An example external redirect:
---
permalink: /leaving/
destination: http://example.com/arriving
canonical: false
---