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