Skip to content

Instantly share code, notes, and snippets.

@domenic
Created February 10, 2017 19:28
Show Gist options
  • Star 110 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save domenic/1f286d415559b56d725bee51a62c24a7 to your computer and use it in GitHub Desktop.
Save domenic/1f286d415559b56d725bee51a62c24a7 to your computer and use it in GitHub Desktop.
Redirecting GitHub pages after a repository move

Redirecting GitHub Pages after a repository move

The problem

You have a repository, call it alice/repo. You would like to transfer it to the user bob, so it will become bob/repo.

However, you make heavy use of the GitHub Pages feature, so that people are often accessing https://alice.github.io/repo/. GitHub will helpfully redirect all of your repository stuff hosted on github.com after the move, but will not redirect the GitHub Pages hosted on github.io.

The solution

We solve this by:

  1. Moving the repository, thus breaking all the links
  2. Creating a new user GitHub Pages site for Alice (not project GitHub pages site), at https://alice.github.io.
  3. Adding a file repo/index.html to the user GitHub pages site, which uses <meta http-equiv="refresh"> to redirect to the new URL.

This means that https://alice.github.io/repo/ still exists as a working URL, even though the alice/repo repository does not. And it will redirect to the new URL, at https://bob.github.io/repo/.

The details

To create a new user GitHub pages site for Alice, Alice will just create a new repository on GitHub named alice.github.io, with a single branch gh-pages. This should work out of the box, although if it doesn't, Alice can check the settings tab for that repository to make sure it's publishing to GitHub Pages.

The file at repo/index.html in the alice/alice.github.io repository should look like this:

<!DOCTYPE html>
<meta charset="utf-8">
<title>Redirecting to https://bob.github.io/repo/</title>
<meta http-equiv="refresh" content="0; URL=https://bob.github.io/repo/">
<link rel="canonical" href="https://bob.github.io/repo/">
@k-hinnenkamp
Copy link

When I do this and click on the old link, it downloads the new file instead of redirecting. Any idea what I am doing incorrectly?

@paulirish
Copy link

Thanks @domenic! The meta refresh with 0 sec delay is great, and agree that canonical is the move.

I also like to add in some body content, but it's definitely optional.

<body style="text-align: center">
Redirecting to <a href="https://bob.github.io/repo/">https://bob.github.io/repo/</a>

@mukhtharcm
Copy link

can i just add this one file and get my github pages personal blog to netlify URL?

@MattPM
Copy link

MattPM commented Nov 13, 2020

thanks this worked perfect for me.

@EvokerKing
Copy link

I did it and it worked. Then I left my PC for a bit, came back and it stopped working. It only works if I go to /repo.

@EvokerKing
Copy link

I did it and it worked. Then I left my PC for a bit, came back and it stopped working. It only works if I go to /repo.

Update: It works now. I removed it from inside of repo

@hancush
Copy link

hancush commented Mar 15, 2021

worked like a charm – thank you!

@pranav-dabre
Copy link

Thank You

@QLVL
Copy link

QLVL commented Apr 1, 2021

This is awesome, thank you!

@Sneezry
Copy link

Sneezry commented Apr 25, 2021

This could be the only solution for this scenario. But I would like to remind people who is reading this. http-equiv refresh could make tons of security software (Avira, CEDF, CyRadar, ESET, Kaspersky, McAfee, Symantec, etc) mark your site as phishing by mistake. And you have to send requests to all of them to remove your site from their block lists. Some of them may just ignore your request, Kaspbersky for example.

@akashennn
Copy link

Initially didn't work then I triggered the deployment again (as per https://stackoverflow.com/questions/45362628/github-pages-site-not-detecting-index-html) -- then it started working, Thanks :)

@rdmolony
Copy link

Thanks @domenic If you anyone also wants to redirect subpages you can create a new file called 404.html with the same contents as index.html

@thumbarnirmal
Copy link

I was looking exactly for this. Thanks! 👍

@MattCopenhaver
Copy link

MattCopenhaver commented Sep 29, 2021

This doesn't seem to handle redirection for different paths within the original site. For instance https://alice.github.io/repo/some-content won't get redirected at all.

Any ideas there? I don't know of an option other than creating a similar index file for each path you support...

@web-apply
Copy link

yes

@nvuillam
Copy link

nvuillam commented Oct 30, 2021

In addition to root redirection as described upper, you can also override 404 error page using JS redirection, that preserves the rest of the URL :) ( full url and not just root domain, query, hashtag...)

Can be seen in its natural habitat there -> https://github.com/nvuillam/nvuillam.github.io/blob/main/docs/overrides/404.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Error 404 :(</title>

    <script>
        window.onload = function () {
            const destination = window.location.href;
            if (window.location.href.includes("nvuillam.github.io/mega-linter")) {
                const redirectLocation = window.location.href.replace(
                    "nvuillam.github.io/mega-linter",
                    "megalinter.github.io"
                );
                document.getElementById("body").innerHTML = "Mega-Linter has moved into an organization repository for its V5 !<br/><br/>Redirecting to " + redirectLocation + " ...";
                window.location.href = redirectLocation;
            }
        }
    </script>
</head>

<body id="body" style="text-align: center">
    Page not found
</body>

</html>

@abmptit
Copy link

abmptit commented Aug 22, 2022

Hello,
I ve got similar problem.
I created repos TOTO, I deployed github page https://USER.github.io/toto
After that I removed the TOTO repos, I recreate another with same name, I deployed github pages,
When I try to navigate to https://USER.github.io/toto I'm redirected to https://laughing-carnival-953f0f88.pages.github.io/
Any idea plz ?
:(

@akaszynski
Copy link

The 404.html is just what I needed to be able to redirect the subpaths of the original site. Thanks!

@PMIRNC
Copy link

PMIRNC commented Oct 27, 2022

Thank you

@vigneshibmpub
Copy link

Hi,

I have renamed my repo from Vignesh-es-test to vignesh-es.

Since it is the same repo, do you have any solution for that?

@oyepriyansh
Copy link

Hi,

I have renamed my repo from Vignesh-es-test to vignesh-es.

Since it is the same repo, do you have any solution for that?

create a repo with name Vignesh-es-test and do these actions with it
@vigneshibmpub

@oyepriyansh
Copy link

This doesn't seem to handle redirection for different paths within the original site. For instance https://alice.github.io/repo/some-content won't get redirected at all.

Any ideas there? I don't know of an option other than creating a similar index file for each path you support...

@MattCopenhaver

create a file named 404.html and add the meta redirect in it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment