Well, it happens. We move a page or a site to make things work better. But what about the people trying to get to the old page? We don’t want to just leave people hanging. They tend to not like that.
Now, we can just use 301 rewrite rules in htaccess, or use nginx redirects. But if we have github or somewhere where we can’t do that, what then?
Well, the next best thing is to use the jekyll-redirect-from to redirect pages. It’s pretty easy.
Note: Many people don’t like MetaRefresh which is the method used here to redirect. See the note at the bottom of the page
Add this to your Gemfile:
Then update your bundle:
Add the plugin
jekyll-redirect-from to your _config.yaml file, making sure to add it to the
whitelist areas if you already have them:
# mimic GitHub Pages with --safe
Then run your jekyll like you always do:
jekyll <cmd> --safe
To use it, just add this to your file’s frontmatter:
title: Your Post
That will create a
/old/page/index.html and an
/old2/page2 page that redirects to the
Your Post page.
Redirect to Other Sites
To redirect to other pages, use:
title: Your Post2
You’ll notice the
redirect_to entry that tells jekyll to create a page for
Your Post2 that will redirect to
Plain and simple.
Warning About MetaRefresh
Many places on the internet say you shouldn’t use MetaRefreshes anymore. And they have valid reasons. This method uses MetaRefresh because there is no other way to do so on GitHub.
If you have your own server, I’d recommend using a Rewrite rule or something else like that to redirect pages transparently without showing the user a redirect page. It’s cleaner, but requires your to edit server config files or htaccess files.