Ever found yourself wanting to redirect all traffic from your GitHub Pages site to a specific page, like your homepage? You're not alone. GitHub Pages offers a simple way to achieve this using a custom 404.html page. Here's how you can do it.

By default, when a user navigates to a non-existent page on your GitHub Pages site, they'll see a standard 404 error page. However, you can customize this experience by creating a custom 404.html file in your repository's root directory. This file will be served whenever a user tries to access a non-existent page.

Creating a Custom 404.html Page
To start, let's create a basic 404.html file. This file should contain HTML, head, and body tags, just like any other HTML file. Here's a simple example:

<!DOCTYPE html> <html> <head> <title>Page Not Found</title> </head> <body> <h1>Oops! This page doesn't exist.</h1> <p>We're sorry, but the page you're looking for can't be found.</p> </body> </html>
Redirecting to Index.html

Now, let's redirect all traffic from this 404.html page to your index.html page. We'll use a bit of JavaScript for this. Add the following script to your 404.html file:
<script> window.location.href = '/index.html'; </script>
This script will redirect the user to your index.html page as soon as the 404.html page loads.

Testing Your Redirect
To test your redirect, push your 404.html file to your GitHub repository and visit a non-existent page on your GitHub Pages site. You should be automatically redirected to your index.html page.
If you're not being redirected, double-check that your 404.html file is in the root directory of your repository and that your JavaScript script is correctly implemented.

Customizing Your 404.html Page
While a simple redirect is useful, you might want to provide a better user experience. You can customize your 404.html page to include a search bar, links to popular pages, or even a fun error message.




















Here's an example of a more customized 404.html page:
<!DOCTYPE html> <html> <head> <title>Page Not Found</title> </head> <body> <h1>Oops! This page doesn't exist.</h1> <p>We're sorry, but the page you're looking for can't be found.</p> <form action="/" method="get"> <input type="text" name="q" placeholder="Search..."></form> <p>In the meantime, you might find what you're looking for on our</p> <ul> <li><a href="/about">About</a></li> <li><a href="/contact">Contact</a></li> </ul> </body> </html>
Using a 404.html Page for SEO
A custom 404.html page can also help with SEO. By providing a useful and relevant page for users to land on when they encounter a 404 error, you can improve their experience and potentially reduce your site's bounce rate.
Moreover, by including a search bar or links to popular pages, you can help users find what they're looking for, even if they've landed on a non-existent page.
And there you have it! You've now learned how to create a custom 404.html page on GitHub Pages and use it to redirect users to your index.html page. Happy coding!