GitHub pages から他サイトにリダイレクト
やりたいこと
GitHub pages でホストしていたドキュメントをRead the Docs に移行したので、GitHub pages からリダイレクトしたい。 ページ構成は同じなのでサブページもリダイレクトしてほしい:
- https://lan496.github.io/hsnf → https://hsnf.readthedocs.io/en/latest/
- https://lan496.github.io/hsnf/definition.html → https://hsnf.readthedocs.io/en/latest/definition.html
手順
- まず
gh-pages
branch のファイルを消す
git checkout gh-pages git rm -r *
index.html
を作って、index page をリダイレクトさせる
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>Redirecting to https://hsnf.readthedocs.io/en/latest/</title> <meta http-equiv="refresh" content="0; URL=https://hsnf.readthedocs.io/en/latest/"> <link rel="canonical" href="https://hsnf.readthedocs.io/en/latest/"> </head> </html>
404.html
でサブページもリダイレクトさせる
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>404</title> <script> window.onload = function () { if (window.location.href.includes("lan496.github.io/hsnf")) { const redirectLocation = window.location.href.replace( "lan496.github.io/hsnf", "hsnf.readthedocs.io/en/latest" ); document.getElementById("body").innerHTML = "Redirecting to " + redirectLocation + " ..."; window.location.href = redirectLocation; } } </script> </head> <body id="body" style="text-align: center"> Page not found </body> </html>
- push する
git add index.html git add 404.html git commit git push origin gh-pages
これでリダイレクトできる
References
- Redirecting GitHub pages after a repository move https://gist.github.com/domenic/1f286d415559b56d725bee51a62c24a7
- https://gist.github.com/domenic/1f286d415559b56d725bee51a62c24a7?permalink_comment_id=3945701#gistcomment-3945701
- Creating a custom 404 page for your GitHub Pages site - GitHub Docs https://docs.github.com/en/pages/getting-started-with-github-pages/creating-a-custom-404-page-for-your-github-pages-site