Let us assume you have created an .asp file called error404page.asp, which is customized to handle HTTP 404 errors. When a visitor to your site tries to access a page that does not exist on your server, say a page called missedpage.asp, then the ASP file will be invoked as if the user had typed:
‘http://www.example.com/error404page.asp?404;http://www.example.com/missedpage.asp’ in the Web browser.
From the information with the above query string, "404;http://www.example.com/missedpage.asp", we can show the user the page they have requested, and send instant information to the administrator. Let us see how this can be done.
Save the file error404page.asp in the root folder of your web site which you have created to handle the HTTP 404 errors. As mentioned in my previous post ‘How to configure Error 404 page in IIS Server’ configure the custom 404 message of your web site to point to the URL, error404page.asp.
To track the URL that triggered the HTTP 404 Error, we have to write a code in the asp page error404page.asp with the help of Request.query string. Then we have to clean the captured query string to get the requested URL and send a mail to the site administrator using CDONTS. We will then display the page to the user after completing all the server side processing.
In the ASP page, error404page.asp, copy the following code to display to the user when a HTTP 404 error occurs:
'error404page.asp - ASP file for handling Customer Error
'Messages mapped to a URL
'As I explained above, the entire query string starts with
'the error number, followed by the requested URL
capturedQryStrg = Request.ServerVariables("Query_String")
'Once we have captured the entire query string, in this
'case "404;http://www.example.com/missedpage.asp", let us
'clean it by removing the "404;", leaving us with just the
requestedURL = Replace(capturedQryStrg, "404;", "")
'After we are done with cleaning up, we will send a mail
'to the site administrator using the CDONTS object.
'We will mark the email as urgent.
Set errMail = Server.CreateObject("CDONTS.NewMail")
errMail.From = "email@example.com"
errMail.To = "firstname.lastname@example.org"
errMail.Subject = "URGENT - 404 Error"
errMail.Body = "User requested - " & requestedURL
errMail.Importance = 2
Set errMail = Nothing
'We are done with the server-side processing.
'We will now display the content to the user.
<Title>Sorry, but <% = requestedURL %> was not found on example.com</Title >
The page you requested, <%= requestedURL %>was not found on
It's possible you typed the address incorrectly, or that the page no
longer exists. <br>
An email alert has been sent to the administrator regarding this error.<BR>
As an option, you may visit any of the pages below for information about our services or products:<BR>