Welcome to WebmasterWorld Guest from

Forum Moderators: ocean10000

Message Too Old, No Replies

301 Redirecting via Javascript code on 404 pages

Have some code will it work?



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

10+ Year Member

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?

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];

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]


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

WebmasterWorld Senior Member 10+ Year Member

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.


Featured Threads

Hot Threads This Week

Hot Threads This Month