A Extra Helpful 404 – A Checklist Aside

Encountering 404 errors isn’t new. Usually, builders present customized 404 pages to make the expertise rather less irritating. Nonetheless, for a customized 404 web page to be actually helpful, it shouldn’t solely present related data to the person, however must also present speedy suggestions to the developer in order that, when attainable, the issue will be mounted.

Article Continues Beneath

To perform this, I developed a customized 404 web page that may be tailored to the appear and feel of the web site it’s used on and makes use of server-side consists of (SSI) to execute a Perl script that determines the reason for the 404 error and takes acceptable motion.

To offer helpful and particular data to the person, it’s essential to outline the attainable causes of a 404 error. Listed here are 4 attainable causes:

  1. The person mistyped the URL or adopted an out-of-date bookmark. These are grouped collectively as a result of we’ll see that it’s not attainable to tell apart one from the opposite.
  2. The person encountered a 404 error due to a damaged hyperlink inside my website.
  3. The 404 error outcomes from a damaged hyperlink returned by a search engine.
  4. The 404 error was attributable to a damaged hyperlink on one other web site, however not a search engine.

In every of those circumstances, the 404 offers details about the precise explanation for the error. If the damaged hyperlink is both on my web site or another person’s web site, however not returned by way of a search engine, the Perl script sends me, the developer, an e-mail in regards to the damaged hyperlink, together with the URL the hyperlink factors to and the web page the person was attempting to succeed in.

SSI help you embody frequent snippets of static HTML, comparable to a header and footer, all through a website. SSI pages, which generally have an .shtml extension, are processed by the server earlier than the pages are despatched to the browser.

When an SSI directive comparable to this one:

<!--#embody digital="/inc/header.html" -->

is encountered within the .shtml file, the server replaces that line with the contents of the file specified.

Nonetheless, along with this relatively easy operate, SSI can execute packages comparable to Perl scripts. On this case, the output generated by the Perl script is shipped to the browser.

Since I needed my customized 404 web page to supply particular data to the person in addition to ship data to me, my customized 404 web page is an .shtml web page during which I take advantage of SSI to execute a Perl script that does all of the work. For my website, the SSI directive seems to be like this.

<!--#embody digital="/cgi-bin/404.pl" -->

The remainder of the 404 web page comprises code to present the web page the appear and feel of the web site that comprises it.

Enabling customized 404 pages#section4

The online server must be configured to make use of SSI. This may be completed by both utilizing an .htaccess file, or modifying the Apache httpd.conf file.

First, to have Apache serve up my particular 404 web page when a 404 error is encountered, I add the ErrorDocument directive to the httpd.conf file, or the .htaccess file. It seems to be like this.

ErrorDocument 404 /errorpages/404.shtml

Second, to inform Apache to execute CGI scripts, I would like to verify the httpd.conf file has the ExecCGI parameter added to the Choices directive. Or I can simply add: Choices +ExecCGI to the .htaccess  file.

The Perl script does the processing to find out the suitable motion. To determine the supply of the 404 error, the Perl script accesses the HTTP_REFERER environmental variable.  HTTP_REFERER comprises the URL of the web page that the person simply got here from. I notice that there are not any ensures that that is correct as a result of it may be faked, however this isn’t actually a priority for this utility.

On the whole, the Perl code performs the next steps:

  1. Test HTTP_REFERER to find out the supply of the 404 error.
  2. Show the suitable message to the person.
  3. Ship me an e-mail message, if wanted for the actual error.

Case 1: Mistyped URL or out-of-date bookmark#section6

Within the case of a mistyped URL or an out-of-date bookmark, the HTTP_REFERER can be clean. In Perl, I test for this utilizing the next code:

if (size($ENV{'HTTP_REFERER'}) == 0)

The Perl script shows a message within the customized 404 web page that tells the person what the issue is. Within the messages exhibited to the person, in addition to any e-mail messages despatched to me, I present the URL of the requested web page utilizing this code:

my $requested = "http://$ENV{'SERVER_NAME'}$ENV{'REQUEST_URI'}";

Case 2: Damaged hyperlink on my web site#section7

When HTTP_REFERER isn’t clean, I test to see if it refers to my website, any individual else’s website, or a search engine. If it comprises my area title, then I do know the person adopted a hyperlink from one among my pages. I take advantage of the next Perl snippet to test for this:

if ((index($ENV{'HTTP_REFERER'}, $ENV{'SERVER_NAME'}) >= 0))

The index operate will return the place of SERVER_NAME within the HTTP_REFERER string. If it’s there, index  can be a quantity better than zero and I’ll know that the person was on a web page on my website.

On this case, I current a message to the person stating that I’ve a damaged hyperlink on my web page. Nonetheless, relatively than ask the person to ship me an e-mail telling me this, the Perl script sends me an e-mail containing the entire essential data. On the similar time, I let the person know that an e-mail has simply been despatched and the damaged hyperlink can be corrected shortly.

Within the e-mail message, I set the topic of the message to obviously determine that there’s a damaged hyperlink on my website and supply the area title utilizing $ENV{'SERVER_NAME'}. This enables me to make use of this script on a number of websites whereas simplifying the sorting of any incoming messages. The physique of the e-mail tells me the URL of the web page the person was on, in addition to the URL of the requested web page.

Case 3: Damaged hyperlink from a search engine#section8

To find out if the person got here from a search engine outcomes web page, I test HTTP_REFERER in opposition to a listing of search engine URLs. This checklist is saved in a easy textual content file that the Perl script reads. By utilizing an exterior file containing a listing of URLs, I can replace the checklist at any time and never have to change the Perl.

Listed here are the Perl snippets for this case:

my $SEARCHENGINE = "false";
open(FILE, "searchengines.txt") or die "can't open the file";
whereas () {
  chomp;
  if (index($referrer, $_) >= 0) {
    $SEARCHENGINE = "true";
  }
}

then,

if ($SEARCHENGINE eq "true")

On this case, I let the person know that the search engine returned an previous hyperlink. Since there actually isn’t something I can do about it, I don’t want an e-mail message, nevertheless, I might want one simply so I learn about the issue.

Case 4: Damaged hyperlink on any individual else’s web site#section9

If the 404 was not the results of any of the three earlier conditions, then I do know it was attributable to a damaged hyperlink on any individual else’s web page. So once more, the Perl script shows the suitable data to the person and sends me an e-mail message. I can then go to the web page with the damaged hyperlink and if the web page proprietor has offered contact data, I can notify them of the issue.

There isn’t any purpose this received’t work on internet servers operating Microsoft IIS. The server must be configured to permit scripts to be executed, and naturally Perl must be obtainable.

Implementing this practice 404 web page improves the usability of my website by serving to the person, and it retains me knowledgeable of damaged hyperlinks. For readability, the desk under reveals the 4 circumstances I mentioned, together with the message exhibited to the person and any e-mail message that’s despatched.

Desk 1: The 4 circumstances mentioned
   

   

   

 

 

 

 

   

   

   

 

 

 

 

   
Case Message to Consumer E-mail message
Case Message to Consumer E-mail message

1. Mistyped URL or out-of-date bookmark

Sorry, however the web page you have been attempting to get to, http://www.mydomain.com/
no-such-page.shtml, doesn’t exist.

It seems to be like this was the results of both a mistyped deal with or an out-of-date bookmark in your internet browser.

You might need to strive looking out this website or utilizing our website map to search out what you have been in search of.

None

2. Damaged hyperlink on one among my pages

Sorry, however the web page you have been attempting to get to, http://www.mydomain.com/
no-such-page.shtml, doesn’t exist.

Apparently, we’ve got a damaged hyperlink on our web page. An e-mail has simply been despatched to the one who can repair this and it ought to be corrected shortly. No additional motion is required in your half.

From: www.mydomain.com 404 script

Topic: Damaged hyperlink on my website, www.mydomain.com.

Message: BROKEN LINK ON MY SITE

There seems to be a damaged hyperlink on my web page, http://www.mydomain.com/
badlink.shtml. Somebody was attempting to get to http://www. mydomain.gov/
no-such-page.shtml from that web page.  Why don’t you check out it and see what’s improper?

3. Damaged hyperlink on a search engine outcomes web page

Sorry, however the web page you have been attempting to get to, http://www.mydomain.com/no-such-page.shtml, doesn’t exist.

It seems to be just like the search engine has returned a hyperlink to an previous web page. These previous hyperlinks ought to finally be faraway from their indexes however since these are routinely generated there is no such thing as a one to contact to attempt to appropriate the issue.

You might need to strive looking out this website or utilizing our website map to search out what you have been in search of.

Elective.  An e-mail message isn’t wanted as a result of there isn’t a lot I can do in regards to the damaged hyperlink however I could go forward and have the script ship me one simply so I learn about it.

4. Damaged hyperlink on any individual else’s web page

Sorry, however the web page you have been attempting to get to, http://www.mydomain.com/
no-such-page.shtml, doesn’t exist.

Apparently, there’s a damaged hyperlink on the web page you simply got here from. Now we have been notified and can try and contact the proprietor of that web page and allow them to learn about it.

You might need to strive looking out this website or utilizing our website map to search out what you have been in search of.

From:www.mydomain.com 404 script

Topic: Damaged hyperlink on any individual else’s website.

Message: BROKEN LINK ON SOMEBODY ELSE’S SITE

There seems to be a damaged hyperlink on the web page, http://www.somedomain.com/
badlink.shtml.  Somebody was attempting to get to http://www.mydomain.com/
no-such-page.shtml from that web page.  Why don’t you check out it and see when you can contact the web page proprietor and allow them to learn about it?


Concerning the Writer

Dean Frickey

Dean Frickey has been concerned within the web since 1995, instructing lessons on the native college. His pursuits are in designing to internet requirements and internet usability. He lives and works in Idaho Falls, Idaho.



Leave a Comment