Setup redirect from HTML page without using JavaScript
This post will discuss how to set up a redirect from an HTML page without using JavaScript.
1. Using Meta Tag
You can redirect your website or its content using a META Refresh. The idea is to place the following in the head section of your HTML page:
<meta http-equiv="refresh" content="0; url=https://www.example.com" />
In the content parameter, https://www.example.com/ should be replaced with the actual web page link, and optionally you may increase the time interval (in seconds) between the page load and redirection.
Here’s a simple example demonstrating this, where a redirect happens to https://www.google.com/ on page load without any lapse:
|
1 2 3 4 5 6 7 8 9 10 |
<!doctype html> <html lang="en"> <head> <meta http-equiv="refresh" content="0; url=https://www.google.com/" /> <title>JavaScript</title> </head> <body> <!-- Your content here --> </body> </html> |
2. Using 301 Redirect
Note that the use of meta refresh is discouraged by the World Wide Web Consortium (W3C). A better and preferred alternative to redirect a user to a different page is to use an HTTP status code, such as HTTP 301 or 302. We can achieve this with a custom rule in the Web server or a script installed on your Web server.
The following example set up a 301 redirect using a .htaccess file at the domain level.
|
1 2 3 4 |
RewriteEngine on RewriteCond %{HTTP_HOST} ^example.com [NC,OR] RewriteCond %{HTTP_HOST} ^www.example.com [NC] RewriteRule ^(.*)$ https://www.google.com/$1 [L,R=301,NC] |
To set up 301-redirect for individual pages, you can do this:
|
1 |
Redirect 301 /oldpage.html /newpage.html |
3. Using <body> tag – onload property
You can also set up a redirect using onload property of the HTML <body> tag. The page will then be redirected as soon as the DOM is loaded and the onload event is fired.
|
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html lang="en"> <head> <title>JavaScript</title> </head> <body onload="window.location='https://www.google.com/'"> <!-- Your content here --> </body> </html> |
That’s all about redirecting from an HTML page without using JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)