How to redirect Github Pages

When you change your Github username, you are greeted with the following warning (emphasis by me):

  • We will not set up redirects for your old profile page.
  • We will not set up redirects for Pages sites.
  • We will create redirects for your repositories (web and git access).
  • Renaming may take a few minutes to complete.

Since there are quite a few websites on the internet linking to pages of my blog, I needed to setup some redirection manually. To do this, I registered a new Github user with my old username Simmesimme and created a repository for the Github pages of that new user. Here are the two repositories:

When you look at those, you can see that the first repository which will be published to my previous URL (https://simmesimme.github.io) contains an html file for each page which existed when I changed my user name. These html files simply redirect to their new location at https://schneegans.github.io.

As there are quite a lot of html files to create, I set up a simple shell script to do this job for me. If you are in a similar situation, this may help you as well!

The Shell Script & the HTML Template

Let’s assume that you have the two repositories cloned next to each other. Let’s call them olduser.github.io (simmesimme.github.io in my case) and renamed.github.io (schneegans.github.io in my case). First create a script called create.sh and a template.html.in file in olduser.github.io. In the template you need to insert four times your renamed.github.io URL.

#!/bin/bash

# This script can be used to create a redirection copy of your
# website! It copies a directory structure containing html files from
# $INPUT_DIR to $OUTPUT_DIR. For each html file in $INPUT_DIR, a copy
# of template.html.in is created in $OUTPUT_DIR. Then, all occurences
# of @FILE@ in the template are replaced with the name of the copied
# file.

# The output dir is assumed to be the location of this script.
OUTPUT_DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $OUTPUT_DIR

# The input dir must be passed as first parameter.
INPUT_DIR="$1"

find $INPUT_DIR -name "*.html" -printf "%P\n" | while read file; do
    mkdir -p $(dirname $file)
    cp template.html.in $file
    sed -i -e "s~@FILE@~$file~g" $file
done
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf8">
  <meta http-equiv="refresh" 
        content="0; url=https://renamed.github.io/@FILE@">
  <link rel="canonical" href="https://renamed.github.io/@FILE@">
  <title>This page has moved</title>
</head>

<body>
  <p>    
    This page has moved. Redirecting you to 
    <a href="https://renamed.github.io/@FILE@">
        https://renamed.github.io/@FILE@
    </a>
  </p>
</body>

</html>

Then you can execute the script. As an argument you give the path to the jekyll-generated _site directory of your renamed.github.io repository.

cd olduser.github.io
chmod +x create.sh
./create.sh ../renamed.github.io/_site

That’s it! Now you have set-up a redirection page for each html file!

Comments

blog comments powered by Disqus