Software engineering notes

GitHub Pages redirection

leave a comment »

Solution

  1. Install the jekyll-redirect-from plugin
  2. update the front matter on posts to include redirect_to: https://new.site/new/path
  3. Load a post and observe the browser redirect to the new location 👍

Problem statement

I was using GitHub Pages (Jekyll) for blogging, but recently switched to WordPress. I didn’t want to break old links, so I needed a way to permanently redirect.

An SO answer got me thinking about a meta tag. Is there an efficient way to add this meta tag to posts? Yes. Some old GitHub Enterprise Pages docs recommend using the jekyll-redirect-from plugin. We can confirm it’s supported for non-Enterprise Pages by looking at the list of supported plugins. And I see it works via a meta tag.

Is the redirect permanent? Sort of. The HTTP response from GitHub is 200, but the HTML redirect includes a canonical link, eg:

$ curl -v https://erikeldridge.com/notes/google-cloud-workstation.html
< HTTP/2 200 
< server: GitHub.com
< content-type: text/html; charset=utf-8
...
<!DOCTYPE html>
<html lang="en-US">
  <meta charset="utf-8">
  <title>Redirecting…</title>
  <link rel="canonical" href="https://blog.erikeldridge.com/2019/03/02/google-cloud-workstation/">
  <script>location="https://blog.erikeldridge.com/2019/03/02/google-cloud-workstation/"</script>
  <meta http-equiv="refresh" content="0; url=https://blog.erikeldridge.com/2019/03/02/google-cloud-workstation/">
  <meta name="robots" content="noindex">
  <h1>Redirecting...</h1>
  <a href="https://blog.erikeldridge.com/2019/03/02/google-cloud-workstation/">Click here if you are not redirected.</a>
</html>

That script tag looks broken, but it’s shorthand for `window.location.href`.

Written by Erik

September 13, 2020 at 10:47 pm

Posted in tool

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: