homepage Welcome to WebmasterWorld Guest from 54.211.219.178
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
301 Redirecting via Javascript code on 404 pages
Have some code will it work?
pastypossie




msg:3830212
 11:02 am on Jan 20, 2009 (gmt 0)

Hi, I'm just checking to see if this is best practice:

I want to 301 redirect pages using the below code on a custom 404 page, so that the code checks to see if its on a specific old URL, and if it is redirect to a new URL.

I'd like to check two things - first of all does the below code look ok?

<%@LANGUAGE="JAVASCRIPT"%>
<%
/**
JScript 404 Page Redirector
13.7.5 Created
**/
var oldURL = new Array();
var newURL = new Array();

var strDomain = "http://www.example.com";
var strRedirect = "/";
var strPath = String(Request.QueryString());

oldURL[0] = '/site/example1';newURL[0] = '/example1';
oldURL[1] = '/site/example2';newURL[1] = '/example2';
oldURL[2] = '/site/example3';newURL[2] = '/example3';
oldURL[3] = '/site/example4';newURL[3] = '/example4';
oldURL[4] = '/site/example5';newURL[4] = '/example5';

for ( var i = 0; i < oldURL.length; i++)
{
if (strPath.indexOf( oldURL[i] ) != -1 )
{
strRedirect = newURL[i];
break;
}
}

strRedirect = strDomain + strRedirect;
Response.Status = "301 Moved Permanently"
Response.AddHeader ("Location", strRedirect);

%>

And what would be the best way to implement it? Should the code above print out HTML in addition, or can I just embed it on a custom 404 page in the site template?

Many many thanks if you can help, I'll shower you in praise and kudos! :)

[edited by: eelixduppy at 1:41 am (utc) on Feb. 10, 2009]
[edit reason] exemplified [/edit]

 

mattur




msg:3832310
 3:57 pm on Jan 22, 2009 (gmt 0)

I haven't checked your code, but it looks like you're on the right track.

You need to check whether the old URL is matched, and if so do the 301 *else* return 404.

Also you need to check how the 404 page is setup in IIS. Depending on how it is configured IIS may return a 302 response first then the error page's status. Can't remember exact details but you can check your script works as expected with the LiveHTTPHeaders extension for Firefox.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved