Ultimate Page of Redirection Techniques
This page has been designed to be the webs resource on redirection techniques. If you know of some useful methods not shown here please let us know. We cover PHP redirections, ASP redirections, Server Side Redirection (301 Redirects) Flash redirection and Javascript redirection including a very sneaky Iframe based redirection trick.
Browser Side Redirection using HTML
In html you can use the meta-refresh tag to redirect a page immediately or after a period of time. A word of warning on this - some search engines will penalise pages that use this page redirection technique with short (less than 300 seconds) delays since they will regard this as a cloaking attempt (human users redirected too quickly to read the page). It can be useful to however to redirect to a different site you own or directly to a sponsors page (with your referral code!) after 350 seconds. The idea being that a surfer has your site in a stack of windows or tabs and as he closes them he will stop at the new page and look at it.
Here is an example of a meta refresh redirect statement with a 350 second delay:
<meta http-equiv="refresh" content="350;url=http://www.disney.com">
This when placed between the head and /head tags of a page would redirect to disney after a 350 second delay. To redirect instantly you simply set the delay to zero.
Browser Side Redirection using Javascript
You can also use javascript to redirect to other pages. This is useful because you can make the redirection condition which is how drop-down navigation menus are created. Of course you can also make this a non-conditional redirect which is a technique often use to cloak content, the search engine will read the page because it does not execute javascript but users will be instantly redirected. Note that this javascript method is rather more instant than 'instant' meta-refresh redirection if the code is placed in the header because meta-refresh waits for the page to load first.
Nowadays the search engines know to look for redirection in javascript so either encrypt the script or load it from a remote file (with acess denied in robots.txt) if you plan on cloaking any content. Here is a simple javascript instant redirect statement:
<script type="text/javascript">
window.location = "http://www.disney.com"
</script>
Browser Side Redirection using Flash
If you make your own flash movies or own a copy of flash this is pretty easy. For an instant redirect make a flash movie with one frame that has a single transparent area in it, then paste the code below into the actionscript pane for that frame. If you want to redirect after a period of time then make a movie of the appropriate length and paste the code into the actionscript pane of the last frame.
getURL("http://www.yoursite.com/somenewpage.htm","_self");
Hidden Browser Side Redirection using Iframe and Javascript
For those who are paranoid about search engines finding their pages are redirected this is a good method. Simply put an iframe in the page you want to redirect from then in the source attribute of the iframe put the url of the page you wish to redirect to.
<iframe width=1 height=1 src=myexampleiframe.htm></iframe>
In the new page you are redirecting to add a javascript framebreaker to its html as below:
<script language="JavaScript" type="text/javascript">
if (self != top) { parent.location.href=self.location.href; }
</script>
So what happens is the browser loads the new page into the iframe, executes that pages framebreaker and displays the new page instead of the one you were originally loading...Bingo!
Server side page redirection using PHP
This is useful when you want to redirect to a different page depending on the outcome of a script. For example this script will redirect to a different page depending on the day of the week:
function dayredirect(){
$day=date('w',time());
#note 0=sunday ....7=saturday
$url=array(
"http://thejokes.co.uk/",
"http://news.bbc.co.uk/1/hi/uk_politics/default.stm",
"http://www.maths.ox.ac.uk/","http://www.ebook-a.com/osman",
"http://video.google.co.uk/videosearch?hl=en&q=fishing",
"http://en.wikipedia.org/wiki/Oil",
"http://www.allthesoccer.com/");
header("Location: $url[$day]");
}
The script will redirect to a joke page on a sunday, political news on a monday and so on. Note that you MUST call the fuction before any text or html is sent to the browser. If you call it after the redirect will fail with an error message.
Page Redirection from any point in a PHP Script
Oh yes you can - if its too awkward to redirect before your script sends headers then here is a way of doing a PHP Redirect after the Header which everybody said could not be done but with a bit of lateral thinking its easy!
Server Side Redirection using .htaccess 301 on Unix servers
Result Code 301 is the only way to redirect to a new page which is properly supported by search engines. It is often known as permanent redirect. Google will transfer not only all traffic but also any pagerank to the new page.
Dot htaccess files are simple text files called '.htaccess' that contain instructions for the server on your site. You can have one file (in the document root directory) for the whole site or one in every directory where it is needed. If you already have a .htacess file in your directory then simply add any redirects to the top of the file (before any other code).
Below are some useful redirect techniques, if you want more information then try the Official .htaccess files documentation. Please note that .htaccess only works if you are on a Unix server. See the next topic on this page for redirecting on a windows server.
To move a single page you can use this line in your .htaccess file:
Redirect 301 /myoldpage.htm http://www.mysite.com/mynewpage.htm
Alternatively you can move an entire site by catching traffic to any page on a disused site and redirecting it to a new site entirely:
Redirect 301 / http://www.awholenewsite.com/
Server Side Redirection using ASP and ASP.net
on windows servers Result Code 301 is the only way to redirect to a new page which is properly supported by search engines. It is often known as permanent redirect. Google will transfer not only all traffic but also any pagerank to the new page.
Place the following code above your <html> tag or <!DOCTYPE> tag if you have one on the page you wish to redirect from:
<%@ Language=VBScript %>
<%
response.status="301 moved permanently"
Response.AddHeader "Location", "http://www.somesite.com/newfile.html"
%>
Of course if you are using .asp.net then the code will be different:
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
response.status = "301 moved permanently";
Response.AddHeader("Location","http://www.somesite.com/newfile.html");
}
</script>
This article first appeared on www.webmaster-a.com.