How to Redirect Webpages to Another on a Website

Page redirection is a very important feature that works well when you have pages on your website that have either been deleted or have never existed. If such pages does not exist on your website, visitors will be shown a 404 error page indicating that the page does not exist.

To make sure visitors are not shown the 404 error page, it is advisable you redirect the page to either the homepage or to another landing page of choice. Another case where redirection is good is in a situation where you just moved from one domain name to another and want to make sure that users to your old domain are automatically redirected to the new domain. A guide on how to redirect one domain to another can be found at How to Redirect a Website Domain to Another.

The code below can be used to redirect any webpage or domain to another. Just follow the easy steps below to learn how to add it to your website;

Click on Button to Redirect Code:

This option informs users to click on a redirect button to be taken to the new webpage or domain. For this;

  • Open the index.html file or the needed file in your favorite text editor.
  • Replace the entire code in the file with the one below.
<html>
   <head>
      <script type = "text/javascript">
         <!--
            function Redirect() {
               window.location = "URL YOU WANT TO REDIRECT TO";
            }
         //-->
      </script>
   </head>
   
   <body>
      <p>The page you are looking for does not exist or has been moved. Click on the button below to be redirected.</p>
      
      <form>
         <input type = "button" value = "Redirect Me" onclick = "Redirect();" />
      </form>
      
   </body>
</html>

Timed Automatic Redirect Code:

The above requires that visitors to the page click on the Redirect button in order to be taken to the expected page. If you want to redirect the users automatically without them having to click on a redirect button, follow the steps below;

  • Open the index.html file or the needed file in your favorite text editor.
  • Search for and locate the <head> tag in the file you opened.
  • Copy the JavaScript code below and paste immediately before/after the <head> tag.
<script type = "text/javascript">
         <!--
            function Redirect() {
               window.location = "URL YOU WANT TO REDIRECT TO";
            }            
            document.write("You will be redirected to main page in 10 sec.");
            setTimeout('Redirect()', 10000);
         //-->
      </script>
  • Finally save the file to apply the change.