Search code examples
htmlgithubhttp-redirectgithub-pages

How to redirect from Github to another site except for the index without using .htaccess


I have a github pages site without jekyll, but with a custom domain (example.com).

Is there a way to redirect all from that site to another site except for the main page (index.html)? E.g. example.com/fun redirects to anotherexample.com/fun but example.com doesn’t redirect anywhere.

Please note that GitHub pages doesn’t support .htaccess

Thanks in advance


Solution

  • A JavaScript way to do this would be to add the following snippet to the <head> section of the pages you want to redirect:

    <script>
      window.location.replace("https://www.example.com");
    </script>
    

    The above script will redirect the pages to https://www.example.com.


    A pure HTML way of doing this would be to add the following meta tag to the <head> section of the pages you want to redirect:

    <meta http-equiv="Refresh" content="0; url='https://www.example.com'" />
    

    The above meta tag will redirect the pages to https://www.example.com after 0 seconds but browsers might not always respond to this meta tag in a same unified way and according to a few seo articles online, search engines may consider your site spammy if you have too many meta refresh redirects.