Tuesday, April 2, 2013

Page Redirection using html,js,php,htaccess


Redirection is two type - client side and  server side. Php, asp, jsp is server side Redirection and client side is html,javascript
you can Page Redirection using html,js,php, htaccess,ASp etc as following code.




Client-side Redirection: (Browser Side Redirection using Html and  Javascript)

1. Using HTML Meta Tag,Code inserted  inside HEAD section:


  Simple url Redirect 
 <meta http-equiv="refresh" content="0;url=http://www.mywebsite.com/urlname.html" />

 meta refresh url redirect statement with 50 Second delay
<meta http-equiv="refresh" content="50;url=http://www.mywebsite.com/urlname.html">

2. Using Javascript 


  You can also using Javascript to perform redirections. It is added  some Javascript code on your HTML pages. The following  below  code  use

 <script>
window.location = 'http://www.mywebsite.com/urlname.html';
</script>

Added the code between your head-tags (<head></head>). You can also redirect using relative URLs:

<script type="text/javascript">
window.location = "../some-dir/urlname.html";
</script>

Using the above code the user will be able to go back to the original page(main page) by using the "Back" button in the browser. This is often undesirable, so given code use:

<script type="text/javascript">
window.location.replace("../some-dir/urlname.html");
</script>


HTML & JavaScript Code: (javascript time delay)

<html>
<head>
<script type="text/javascript">
<!--
function delayer(){
    window.location = "../javascriptredirect.php"
}
//-->
</script>
</head>
<body onLoad="setTimeout('delayer()', 5000)">
<h2>Prepare to be redirected!</h2>
<p>This page is a time delay redirect, please update your bookmarks to our new 
location!</p>

</body>
</html>


Redirection using Flash
getURL("http://www.yoursite.com/somenewpage.htm","_self");

Redirection using Iframe and Javascript


<iframe width=1 height=1 src=myurliframe.html></iframe>

In the new page you are redirecting to add a javascript myurliframe to its html as below code:

<script language="JavaScript" type="text/javascript">
if (self != top) {
parent.location.href=self.location.href;
}
</script>



Server-side Redirection:



Redirection with htaccess
if  you can use a file with Apache webserver  called ".htaccess" to perform redirections. In the htaccess file you can use so-called directives or commands. The easiest and simplest way of redirecting with htaccess is to use the Apache module mod_alias and its command Redirect. By default 302  temporary redirction

Redirect /oldurl.html http://www.mywebsite.com/newurl.html

To make a permanent 301 redirection use:

Redirect 301 /oldurl.html http://www.mywebsite.com/newurl.html

PHP/Server Side Redirect:

Redirect using PHP is done using header() function. by default  temporary 302 redirection from PHP:


<?php
$loc = 'http://www.mywebsite.com/newurl.html';
header("Location: $loc");
die(0);
?>

or


<?php
header('Location: http://www.mywebsite.com/');
exit;
?>


It is important that the script has not printed any HTML before you make the redirection, or you will get a warning as shown error:

Cannot modify header information - headers already sent by ...


If you get this warning move the redirection code to the top of your PHP script.

To make a permanent 301 redirection using PHP:

<?php
header('Location: http://www.mywebsite.com/', true, 301);
exit;
?>



Redirection using ASP on windows servers

<%@ Language=VBScript %>
<%
response.status="301 moved permanently"
Response.AddHeader "Location", "http://www.somesite.com/newfile.html"
%>

Redirection using ASP.net on windows servers

private void Page_Load(object sender, System.EventArgs e)
{
response.status = "301 moved permanently";
Response.AddHeader("Location","http://www.somesite.com/newfile.html");
}
</script>



No comments:

Post a Comment