I want to redirect /old/folder/struct/* to /new/fold/struc/*.
I know that jekyll runs at compile time so it can't really catch the URLs at runtime. I'm looking for a creative workaround.
And preferably, one that would work on GitHub Pages (which means htaccess is not possible).
I'm using a simple meta refresh, built into my default layout file.
You need to do this for every single file, though. There's no "catch-all" solution.
Simple example layout file:
<!DOCTYPE html>
<html>
<head>
<title>{{ page.title }}</title>
{% if page.redirect %}<meta http-equiv="refresh" content="0; url={{ page.redirect }}">{% endif %}
</head>
<body>
<h1>{{ page.title }}</h1>
{% if page.redirect %}<h3> Redirecting to <a href="{{ page.redirect }}">{{ page.redirect }}</a> ...</h3>{% endif %}
{{ content }}
</body>
</html>
<head> will do the actual redirect.With a layout file like this, you can create a file that will redirect to another URL with just two lines of YAML front-matter:
---
layout: default
redirect: /new/fold/struc/foo.html
---
That's all.
As I said in the beginning: you need to do this for each of the hundreds of files in the old folder.
I had the same problem as well, so I wrote a one-off script to do it.
I've also written a blog post about this approach:
Easy meta redirects with Jekyll
Take a look at jekyll-redirect-from.
Just add to your _config.yml:
gems:
- jekyll-redirect-from
Or add it to your Gemfile:
gem 'jekyll-redirect-from'
Then for any page or post you want to redirect:
title: My amazing post
redirect_from:
- /old/folder/struct
- /new/fold/struct
I do notice you have a wildcard, so there may be some other URLS under the older folder structure, in which case I would recommend doing a find and replace in your project.
This works under the condition that you have a setting with the path to the old path. In this case this was an old Blogger post which has blogger_orig_url with the old URL.
This example redirects the path to the html file to Jekyll's new URL format.
I don't think there's an easy solution within Jekyll, as it primarily generates a static website.
In terms of find and replace, you'll need to search for text/instances where the old path exists and change it to the new path-- basically add the redirect_from.
Sample post in _posts/ folder:
---
layout: post
title: 'My New Post'
date: '2005-01-04T10:29:00.002-06:00'
author: John Doe
tags:
- Music
modified_time: '2005-01-11T04:05:03.224-06:00'
thumbnail: http://foobar.com/foo.jpg
blogger_orig_url: http://www.foobar.com/2005/01/my-new-post.html
redirect_from: /2005/01/my-new-post.html
---
Sample text here. This post should be under path /2005/01/my-new-post/.
I don't know exactly what your posts/pages have or look like, I did something like this example when I converted an old Blogger posts to Jekyll's and didn't want to break the old links to my posts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With