scriptygoddess

21 Apr, 2004

ASP: 404 handling

Posted by: Jennifer In: Scripts

I recently published a new site (in ASP), and took down the old pages. Some of them had been updated (with new page names) – others were simply going away. Just as I had done with PHP – I wanted a simple 404 page (in ASP) that would email me when people tried to hit the old pages. That way I could decide if I wanted to set up a redirect for some pages – but in all cases, I didn't want to show a 404 – (I'd just send them back to the home page). I was only interested in htm, htmls, and asp type pages (not interested in missing .ico or gifs)… so here's the script I came up with for my custom 404 page:

(That long explanation was basically to give you the idea that there's probably better/other ways to do this – but I had a specific goal in mind. You're welcome to post alternate, more elaborate methods in the comments. Just thought I'd give you the background first. I should also note that I am NOT anything even CLOSE to an expert on ASP. I only need to use it occasionaly, and I fumble my way through it the best I can – usually, very ungracefully.)

<%
Dim txtToMail, sMissingFile, partstr, partstr2, partstr3, RefURL
sMissingFile = lcase(trim(replace(request.servervariables("query_string"),"404;","")))
'by using lcase – it makes the link all lowercase – so in your checks below
'make sure youre only looking for pages in lowercase (even if the page originally was upper&lowercase
partstr = right(sMissingFile,3)
partstr2 = right(sMissingFile,4)
partstr3 = right(sMissingFile,10)
RefURL = Request.ServerVariables("HTTP_REFERER")
if partstr3 = "cltreq.asp" then
partstr = "xxxxxx"
end if
'this is some weird page I kept getting error notices for – it was never on the site
'either way – I didnt want to get these emails
'below I list any pages and/or directories I removed
if Instr(sMissingFile,"somedirectorynamethatwasremoved") > 0 then %>
<html>
<head>
<meta http-equiv="refresh" content="0;URL=http://www.YOURSITE.com/newpage.asp">
<title></title>
</head>
<body></body>
</html>
<% elseif Instr(sMissingFile,"somespecificpagename.htm") > 0 then %>
<html>
<head>
<meta http-equiv="refresh" content="0;URL=http://www.YOURSITE.com/newrevisedpage.asp">
<title></title>
</head>
<body></body>
</html>
<% elseif Instr(sMissingFile,"adifferentdirectoryname/specificpage.html") > 0 then %>
<html>
<head>
<meta http-equiv="refresh" content="0;URL=http://www.YOURSITE.com/someOtherPage.asp">
<title></title>
</head>
<body></body>
</html>
<% elseif partstr = "htm" or partstr2 = "html" or partstr = "asp" then
'if its none of the above – email me and redirect to the main home page
Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.To = "YOU@YOURDOMAIN.COM"
objCDO.From = "YOU@YOURDOMAIN.COM"
txtToMail = "404 error for this page: " & sMissingFile & vbCrLf & vbCrLf & "Referring URL was: " & RefURL
objCDO.Subject = "404 Error on MY SITE"
objCDO.Body = txtToMail
objCDO.Send
%>
<html>
<head>
<meta http-equiv="refresh" content="0;URL=http://www.YOURSITE.com/index.asp">
<title></title>
</head>
<body></body>
</html>
<% else %>
<html>
<head>
<meta http-equiv="refresh" content="0;URL=http://www.YOURSITE.com/index.asp">
<title></title>
</head>
<body></body>
</html>
<% end if %>

(This was a hodge-podge of several scripts I found online… if I find the links again to specifically which ones, I'll include links to them. However, I've strayed so much from the original (and left so much out that they had) that it's almost not worth mentioning)

update 5/3/04: added referring url info

3 Responses to "ASP: 404 handling"

1 | Ed

April 29th, 2004 at 12:27 pm

Avatar

Jennifer – a concern that some people (and by some people I mean over at A List Apart) have with redirecting someone to the home page if the page cannot be found is that the visitor is confused why the ended up at the home page and they may repeat their unfortunate steps again thinking that they must have done something wrong. When really, they probably haven't. Not that I necessarily think that my ASP 404 is better – it's just a different way to do it that some of your visitors may also find useful.

2 | Tony

September 1st, 2004 at 9:10 am

Avatar

I like your site: nice design and fast too…

I've tried to use this script in Chilisoft ASP, but cannot get querystring or HTTP_REFERER to return the name of the page that the browser was looking for…

Any ideas?

3 | Pete Dobbs

January 18th, 2005 at 10:18 am

Avatar

Redirecting to the home page doesn't have to happen straight away – use a little javascript alert to tell the user what's going on while you log all the details to a file and then redirect either back to the homepage or back where they came from.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements