Redirecting GitHub Pages

Today I finished moving the Edge Tools for VS Code extension documentation to its official space in the Microsoft docs. That meant I needed to redirect the documentation I hosted with GitHub pages in a docs folder/branch of the repository.

There are p…


This content originally appeared on DEV Community and was authored by Christian Heilmann

Today I finished moving the Edge Tools for VS Code extension documentation to its official space in the Microsoft docs. That meant I needed to redirect the documentation I hosted with GitHub pages in a docs folder/branch of the repository.

There are plugins available for that, but I didn't want to install any extra features on the repository, so I chose a simpler approach.

You can define HTML templates for your GitHub pages in a folder called _layouts and connect them using the Markdown frontmatter. So if you create a file called test.md you can define a template called forward. You can also add a target to redirect to, in this case https://example.com

test.md:

--------
layout: forward
target: https://example.com
--------
... rest of your markdown ... 

Your forward.html template in the _layouts folder can use a meta redirect to the target. In its most basic form this can be:

forward.html:

<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="refresh" content="0;url={{ page.target }}"/>
    <link rel="canonical" href="{{ page.target }}"/>
    <title>Redirecting</title>
</head>
<body>
    <p>Document has moved, if you aren't automatically redirected 
    <a href="{{ page.target }}">go here</a>.</p>
</body>
</html>

That means that if someone goes to the test document in your GitHub pages, they will get redirected to example.com

You can see this in action in this GitHub demo repo I quickly put together. I've added quite a few more options to redirect, such as definition of the time and displaying different titles and text. You can read it all and try out the demos in the README.


This content originally appeared on DEV Community and was authored by Christian Heilmann


Print Share Comment Cite Upload Translate Updates
APA

Christian Heilmann | Sciencx (2021-08-03T17:14:50+00:00) Redirecting GitHub Pages. Retrieved from https://www.scien.cx/2021/08/03/redirecting-github-pages/

MLA
" » Redirecting GitHub Pages." Christian Heilmann | Sciencx - Tuesday August 3, 2021, https://www.scien.cx/2021/08/03/redirecting-github-pages/
HARVARD
Christian Heilmann | Sciencx Tuesday August 3, 2021 » Redirecting GitHub Pages., viewed ,<https://www.scien.cx/2021/08/03/redirecting-github-pages/>
VANCOUVER
Christian Heilmann | Sciencx - » Redirecting GitHub Pages. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/03/redirecting-github-pages/
CHICAGO
" » Redirecting GitHub Pages." Christian Heilmann | Sciencx - Accessed . https://www.scien.cx/2021/08/03/redirecting-github-pages/
IEEE
" » Redirecting GitHub Pages." Christian Heilmann | Sciencx [Online]. Available: https://www.scien.cx/2021/08/03/redirecting-github-pages/. [Accessed: ]
rf:citation
» Redirecting GitHub Pages | Christian Heilmann | Sciencx | https://www.scien.cx/2021/08/03/redirecting-github-pages/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.