Moving posts in Jekyll in coding
I just launched this new blog and immediatly made a huge mistake: I typo'ed the date for the first post … and every subsiquent date thereafter.
This created an interesting problem … I had already shared the link to those posts and Google had already indexed it. With a statically built site, how can I move the posts, but redirect the old url to the new location?
I added this to my _includes/head.html
file (just above the </head>
):
{% unless page.new_location == blank %} | |
<meta http-equiv="refresh" content="0; url={{ page.new_location }}"> | |
<link rel="canonical" href="{{ page.new_location }}" /> | |
{% endunless %} |
Then, created this new _layouts/moved.html
file:
--- | |
layout: default | |
--- | |
<header class="post-header"> | |
<h1 class="post-title">Moved</h1> | |
</header> | |
The content you're looking for has been moved to <a href="{{ site.baseurl }}{{ page.new_location }}">{{ page.new_location }}</a>. |
Now, I can create new pages (I can't create posts, because they will be listed in the index page .. so, posts win here) that look similar to this:
--- | |
layout: moved | |
new_location: /meta/2014/08/new-new-blog/ | |
permalink: /meta/2014/09/new-new-blog/ | |
--- |
The end result is a surprisingly flexible way to redirect users (and inform search engines) to relocated content.
Update: It was pointed out to me (by @iamcarrico on the Jekyll IRC Channel) that there is a plugin created by the Jekyll team that handles redirects that's far simplier and easier to use. However, I'm going to continue using my thing, only because I show a pretty screen if the redirect doesn't happen.