C and S Design.
Search Friendly Programming and Design

Go to -> C and S Design -> Articles -> Coding -> Redirect -> Other Redirect -> Show Code

Click here to view or download the code for this example


Coding META and Javascript Redirects


Redirecting pages with META refresh or Javascript

Using meta refresh or javascript to redirect user agents is of little use for having search engine crawlers index the new location. The only user agents to trigger javascript events or to follow the meta action are browsers.

Update on Meta Refresh

A recent update to meta refresh came out of the Dec 2004 SES Chicago, as reported here by Ron Carnell.

Quote Ron Carnell.

A meta-refresh, coded directly in the page, can now take the place of a server-side redirect. Considering how badly meta-refreshes have been abused by spammer in the past, this is surprising news. But it's also good news, especially for the many webmasters who don't have access to server level redirects. A meta-refresh of 1 second or less will be treated as a 301, while anything longer than 1 second will be considered a 302.

META Redirect

A redirect at page level using meta refresh is a fairly simple piece of code;

<meta http-equiv="refresh" content="delay;URL=location">
Where location is the page or site to be redirected to and delay is an integer indicating the number of seconds before the redirection is effected.

<html>
<head>
<title>Redirected</title>
<meta http-equiv="refresh" content="0;URL=http://www.domain.com">
</head>
<body>
This page has moved to
<a href="http://www.domain.com">http://www.domain.com</a>
<br>
<br>
Please update your bookmarks to suit
</body>
</html>

Javascript Redirect

At it's simplest a javascript redirect is a single line of code that will set the location of the current window to a new url as soon as the page is opened with a javascript enabled user agent.


<html>
<head>
<title>Redirected</title>
<script type="text/javascript">
<!--
window.location="http://www.domain.com/";
// -->
</script>
</head>
<body>
This page has moved to
<a href="http://www.domain.com">http://www.domain.com</a>
<br>
<br>
Please update your bookmarks to suit
</body>
</html>

Word of Warning

The examples above would redirect instantly to the new location. Something to be aware of is that search engines can view the use of fast meta and javascript redirects with some suspicion. It is simple enough to detect using an algorithm so could be subject to being filtered or penalised without a code review.

Delayed Javascript Redirect

The example code below is a javascript redirect with a 5 second delay before the redirect. The time is set in the onLoad event call in the <body> tag. javascript times are set in millisecond so 5000 equates to 5 seconds.

JS Delay Demo


<html>
<head>
<script type="text/javascript">
<!--
function redir_delay(){
document.location = "http://www.domain.com"
}
//-->
</script>
</head>
<body onLoad="setTimeout('redir_delay()', 5000)">
This page has moved to
<a href="http://www.domain.com">http://www.domain.com</a>
<br>
<br>
Please update your bookmarks to suit
</body>
</html>

Links to new location

In all cases the indexable content should be removed from the page and just a link to the new location put on the page. This will give the search engines a pointer to the new location and provide a clickable link for any visitors using agents or browsers that do not follow the redirect automatically.


Valid HTML 4.01! Valid CSS! copyright © C and S Design 2004 - 2005
Website Design and SE Friendly Coding C and S Design