Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with error handling 404.asp 2

Status
Not open for further replies.

leeboycymru

Programmer
Jan 23, 2007
185
GB
I followed a thread titled ' and got out of it the code below:

<%
dim i, newURI, req, pageArray(1,50)

pageArray(0,0) = "test.htm"
pageArray(0,1) = "test.asp"

bFound = false
for i = 0 to ubound(pageArray,1)
if instr(request.servervariables("QUERY_STRING"), pageArray(0,i)) > 0 then
req = replace(request.servervariables("QUERY_STRING"),"404;","")
newURI = replace(req,pageArray(i,0), pageArray(i,1))
bFound = true
end if
if bFound then exit for
next
if bFound then
' uncomment the next line to keep the original URI but show the new content
'server.transfer("/newdir/" & pageArray(1,i))
'server.transfer(pageArray(1,i))
' uncomment the next lines to redirect to the new pagename
response.status = "301 Moved Permanently"
response.addheader "Location", newURI
response.end()
else
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html>
<head>
<title>Not Found</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
This is the 404 page
<body>
</body>
</html>

<%
response.status = "404 Not Found"
end if
%>


Im trying to do exactly the same, because i did a website and some of the .htm pages no longer exist, instead replaced by .asp pages.

So I went into my fasthosts control panel and set to that any 400, 401.. 404 etc errors go to my 404.asp error handling page.

To test this I created a page and titled it test.asp, and tested it by typing ' but nothing happened, it simply refreshed the page and stayed on the home page.

Can somebody explain to me, where I am going wrong, and if possible what i need to do.l
 
sticking to the one thread would be a help to start with.

it simply refreshed the page and stayed on the home page.

Somewhere along the way your server custom 404 error settings are wrong.



Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Ok thanks ChrisHirst,

you think the problem is with the hosts?

Lee
 
Ok brill, I got that sorted out...

My next problem is my own lack of experience, and so can anybody help me with this.

Above the 404 handling script deals with one page missing, how can I change that code so that it deals with 6 or so pages missing.

This has happened because I have condensed 5 or so pages into one asp page that deals with the content via IF statements, so leaving a load of links in search engines without anywhere to go.

Can anybody help me with this.

cheers

Lee
 
If you look in my original code you will see that the page name array is dimensioned to 50 but only 2 are implemented.

If you implement more array elements it will handle more pages.
Code:
pageArray(0,1) = "/dir/pagename.htm"
pageArray(1,1) = "/dir/pagename.asp"
pageArray(0,2) = "/dir/pagename.htm"
pageArray(1,2) = "/dir/pagename.asp"
' ....

'pageArray(0,n) = "/dir/pagename.htm"
'pageArray(1,n) = "/dir/pagename.asp"

If you make the second column (1,n) of the array all the same you can combine several hundred pages into 1.

Bear in mind you cannot use querystrings if you are using the server.transfer method. There you have to set your variables in the 404.asp before calling the transfer.






Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Thanks Chris,

I sort of looked at those arrays and thought similarly, but what confused me was in the main bit of code you are using 0 and 1 as in:

for i = 0 to ubound(pageArray,1)

so it hought that was very specific to just those 2 original arrays I was using.

Is this able to deal with the added arrays in the same way?

lee
 
Would the added arrays look like this, and would the main bit of code be able to handle them:

<%
dim i, newURI, req, pageArray(1,50)

pageArray(0,0) = "test.htm"
pageArray(0,1) = "test.asp"
pageArray(1,1) = "test2.htm"
pageArray(1,2) = "test2.asp"
pageArray(2,2) = "test3.htm"
pageArray(2,3) = "test3.asp"
pageArray(3,3) = "test4.htm"
pageArray(3,4) = "test4.asp"
pageArray(4,4) = "test5.htm"
pageArray(4,5) = "test5.asp"
pageArray(5,5) = "test6.htm"
pageArray(5,6) = "test6.asp"

bFound = false
for i = 0 to ubound(pageArray,1)
if instr(request.servervariables("QUERY_STRING"), pageArray(0,i)) > 0 then
req = replace(request.servervariables("QUERY_STRING"),"404;","")
newURI = replace(req,pageArray(i,0), pageArray(i,1))
bFound = true
end if
if bFound then exit for
next
if bFound then
' uncomment the next line to keep the original URI but show the new content
'server.transfer("/newdir/" & pageArray(1,i))
'server.transfer(pageArray(1,i))
' uncomment the next lines to redirect to the new pagename
response.status = "301 Moved Permanently"
response.addheader "Location", newURI
response.end()
else
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html>
<head>
<title>Not Found</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
This is the 404 page
</body>

</html>

<%
response.status = "404 Not Found"
end if
%>
 
BTW The code you posted here is an earlier version complete with bugs.

Got to Redirect XP (ver 3.21) and use that code

ubound(pageArray,2) will get the maximum size of the array as declared, so here it will return 50.
I have it as ubound() because I normally use this code with a DB driven site that can check the database for deleted/replaced items and feed it straight into the 404 page. So the size of the array may vary from day to day.

to extend the array it is

Code:
pageArray(0,0) = "/dir/pagename.htm"
pageArray(1,0) = "/dir/pagename.asp"
pageArray(0,1) = "/dir/anotherpage.htm"
pageArray(1,1) = "/dir/anotherpage.asp"
pageArray(0,2) = "/dir/pagename3.htm"
pageArray(1,2) = "/dir/pagename3.asp"
pageArray(0,3) = "/dir/yetanotherpage.htm"
pageArray(1,3) = "/dir/yetanotherpage.asp"

This code seems to be getting popular, Maybe I should write up an explanation and get it up where it was supposed to be originally [smile]



Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Hi Chris,

I seem to be having a few problems with the code below:

<%
dim i, newURI, req, pageArray(1,50)

pageArray(0,0) = "lee.htm"
pageArray(1,0) = "lee.asp"
pageArray(0,1) = "lee2.htm"
pageArray(1,1) = "lee2.asp"
pageArray(0,2) = "lee3.htm"
pageArray(1,2) = "lee3.asp"
pageArray(0,3) = "lee4.htm"
pageArray(1,3) = "lee4.asp"
pageArray(0,4) = "lee5.htm"
pageArray(1,4) = "lee5.asp"
pageArray(0,5) = "lee6.htm"
pageArray(1,5) = "lee6.asp"

bFound = false
for i = 0 to ubound(pageArray,1)
if instr(request.servervariables("QUERY_STRING"), pageArray(0,i)) > 0 then
req = replace(request.servervariables("QUERY_STRING"),"404;","")
newURI = replace(req,pageArray(i,0), pageArray(i,1))
bFound = true
end if
if bFound then exit for
next
if bFound then
' uncomment the next line to keep the original URI but show the new content
'server.transfer("/newdir/" & pageArray(1,i))
'server.transfer(pageArray(1,i))
' uncomment the next lines to redirect to the new pagename
response.status = "301 Moved Permanently"
response.addheader "Location", newURI
response.end()
else
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html>
<head>
<title>Not Found</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="robots" content="noindex,nofollow">
</head>

<body>
This is the 404 page
</body>

</html>

<%
response.status = "404 Not Found"
end if
%>

If I try to look for a one of the .htm links above so that it will redirect to the .asp version the page takes an age to load and then it results in a time out error.

But if i look for a page that doesnt exist not in the array above, I get taken to the 404.asp error page straight away.

See if does the same with you:


Thanks

lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top