Home Coding & Programming How to Redirect Webpages to Another on a Website

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

List of All JavaScript Arithmetic Operators With Examples

JavaScript offers several operators to help us work with numbers. All these operators are binary operators, which means they operate on two operands. The operands...

How to Find a Website’s RSS Feed’s URL

Having access to the RSS feed of a website gives you the opportunity to stay up-to-date on everything that website publishes. Whether that’s a...

Laravel: String data, right truncated: 1406 Data too long for column (SOLVED)

I was working on a project today when i ran into the error message "String data, right truncated: 1406 Data too long for column...

How to Enable or Disable Chrome’s Autofill on Android Devices

Storing your login details and other information in Chrome and/or your Google Account isn’t as secure as using an encrypted password manager, but it’s...

Recent Comments