Excessively Adequate

Quick and Dirty Redirect of a Jekyll Blog to Its New Domain

Having recently moved this blog from its previous domain to the current1 location, I was on the lookout for a way of easily redirecting all pages of the old instance to their equivalents of the new one, thus avoiding breaking any links.

To achieve this, I didn’t want to install a plugin, futz with the web server configuration, or change the DNS settings of the old domain – partially because I’m lazy and my blog isn’t nearly popular enough to warrant a truly robust solution (see the caveats at the bottom of this post). There’s also my preference for solutions that work universally – for Jekyll sites that one might want to move off of, say, GitHub Pages, where you’d only have access to the DNS settings if using a custom domain, and zero control of the web server configuration or Jekyll plugins no matter what.

At least for my purposes – a low-traffic blog that’s only updated a couple of times each year – the approach outlined below successfully walks the ride line between simplicity and elegance.

Here’s how!

Create a snippet _includes_/domain_redirect.html with the following code which, if a site-wide configuration option has a non-empty value, redirects visitors via <meta http-equiv="refresh" …> and robots with <link rel="canonical" …>:

{% if site.domain_redirect %}
    <meta http-equiv="refresh" content="0; url={{ site.domain_redirect }}{{ page.url }}">
    <link rel="canonical" href="{{ site.domain_redirect }}{{ page.url }}" />
{% endif %}

In the template2 that generates your site’s <head> – depending on your theme, it might be resident at _includes/head.html, _layouts/default.html, or something similar – add the following line somewhere between <head> and </head>, ideally close to the top:

{% include domain_redirect.html %}

Finally, in your old site’s _config.yml, specify the new root URL without a trailing slash:

domain_redirect: https://excessivelyadequate.com

If you ever want to disable the redirect in the future, just remove that line from your configuration again.

Caveats

Of course, a basic approach like this has a number of downsides:

Despite these caveats, most search engines will follow this kind of redirect without any problems, and others might defer to the <link rel="canonical" …> tag.

  1. See how I’m keeping the copy intentionally vague in case I decide to move again? 

  2. If your site is complex, there might even be multiple templates that serve this role for different page types.