Ask Mommy and get Answers from experts
0 votes
I want to automatically redirect a Web Page to another URL. That is when a visitor comes to the old URL he will be automatically redirected to the new URL.
in Websites by Newbie (320 points)

1 Answer

0 votes

You can automatically redirect a web page to another URL using either HTML meta tags or JavaScript. Here's how to do it using both methods:

1. HTML Meta Tag Redirect:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="refresh" content="5;url=https://www.example.com"> <title>Redirecting...</title> </head> <body> <p>This page has moved to <a href="https://www.example.com">example.com</a>.</p> <p>If you are not redirected automatically, <a href="https://www.example.com">click here</a>.</p> </body> </html>

  • In the <meta http-equiv="refresh" content="5;url=https://www.example.com"> line, content="5; means the page will redirect after 5 seconds. You can adjust the number according to your preference.
  • Replace https://www.example.com with the URL you want to redirect to.

JavaScript Redirect:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="refresh" content="5;url=https://www.example.com"> <title>Redirecting...</title> <script> window.onload = function() { setTimeout(function() { window.location.href = "https://www.example.com"; }, 5000); // 5000 milliseconds = 5 seconds }; </script> </head> <body> <p>This page has moved to <a href="https://www.example.com">example.com</a>.</p> <p>If you are not redirected automatically, <a href="https://www.example.com">click here</a>.</p> </body> </html>

  • In the JavaScript code, window.location.href = "https://www.example.com"; redirects the page to the specified URL after 5 seconds (5000 milliseconds).
  • You can adjust the timing by changing the value in setTimeout().
by Silver (3.3k points)
edited by
44 questions
44 answers
7 users