RSS

Web Page Redirection Script – HTML, PHP, JavaScript

Sun, Mar 8, 2009

Hints and Tips, PHP, Programming

Here is the three different approaches to Web Page Redirection. Most of them looking for redirecting visitors to a new page. This tutorial with sample code will explain in-depth of browser window redirected a new page. This is most useful for webmasters or developers and visitors.

HTML Page Redirect:

This is simple and most easiest way to redirect a web page to another page. If you need a pure HTML based web page redirect code means here is the only way to use a META tag refresh to redirect your visitors to another page or another domain.

<meta HTTP-EQUIV=”REFRESH” content=”0; URL=http://www.domainname.com/redirect_page.html”>

Usage: Here you should replace URL value with your redirection page.  Then the CONTENT is time in seconds, o(zero) represents immediate page redirection, you can set a different seconds for delaying Browser redirect. You should keep this META tag inside <head> … </head> html tag otherwise it will not redirect properly.

The Meta Tag based web page redirection is best when you want to show some minimal information to website audience before entering in to a new web page. Also it preserves your website’s page with enough ranking.

A small disadvantage of using meta type REDIRECT page is it does not stop even the user desired to stop redirect refresh until otherwise they reach newer page. You can’t use this method for 404 page redirection, for this we have another kind of web page redirection below.

Javascript Redirect Page:

The JavaScript web page redirecting is another way of redirecting a browser web page to new page.
There are few different methods for redirecting a web page using JavaScript.
The first one is simple script code to be put inside <head> tag or <body> tag of your html page.

<script type=“text/javascript”>
location.replace(‘http://www.domainname.com/’);
</script>

The one line code to page redirection using JavaScript is:

<body onload=”javascript:window.location.href=’http://www.websitename.com/web_page.html’”>

the above script loads while html page loading then readirects to another page immediately.

The another one is using location.replace method,

<script type="text/javascript">
location.replace('http://www.example.com/');
</script>

It does similarly like previous one location.href redirect.

<html>
<head>
<script type=”text/javascript”>
<!–
function pageRedirect(){
window.location = “http://website.com/somepage.html”
}
//–>
</script>
</head>
<body onLoad=”setTimeout(‘pageRedirect()’, 5000)”>
<h2>You will be Redirected shortly!</h2>
<p>We have moved our webpage for some reasons please be visit here!</p>
</body>
</html>

The last redirection example show how a web page loaded with another one with timeout information to visitors.

Web Page Redirection Using PHP Script

To redirect your visitors to a new web page without any reload action from viewers is possible from PHP HTTP redirect using header(); function.

This will be helpful when login redirection, redirect to post forms. The main concept of redirect windows to another page is to bring visitors knowingly or unkowingly to a customized page. The PHP with apache support can do this web page redirections for you.

The code for PHP redirection Script page,

<?php
$redirect_page="http://www.example.com/page.html";
header
("Location: ".$redirect_page);
exit;
?>
Usage: The important constrains for redirecting a page using PHP is, there is no executable code before the function header();.
You should replace the $redirect_page with your own URL for proper redirection.

Popularity: 92%

, , ,

This post was written by:

apsam29 - who has written 24 posts on Techinfoworld Blog.


Contact the author

Leave a Reply

Switch to our mobile site