Skip to content
Advertisement

How to redirect to homepage from any path in github pages?

I tried to redirect to homepage by adding this meta tag inside 404.html file :

 <meta
      http-equiv="refresh"
      content="0; URL='https://gittestfor.github.io'"
    />

And it works fine the first time but the second the page gets cached it doesn’t work .

When I clear site data in the application tab in developer console , it works .

enter image description here

How can I disable this behaviour or redirect from any path to homepage “/” with github pages ?

Advertisement

Answer

I solved the issue by adding a 404.html file to the root of the project in github pages repo and added this code to that file :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      http-equiv="refresh"
      content="0; URL='https://username.github.io'"
    />
  </head>
  <body></body>
</html>

Now every request other than https://username.github.io/ like .../name will be redirected to the homepage which is / .

Note : This behaviour works only when the page is not cached in the browser and there is no service worker to cache the website .

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement