October 29, 2016

Smarter Errors

Once, a long, long time ago, I came across a custom 404 error page using mod_speling. It was a photography page and I was looking for the embedded jpg. The website was using a number of javascript and CSS tricks to beautify the page layout, and in the process disabled to right button mouse click over an image. Instead I viewed the source to get the image URL. When I copied it into the address bar, however, I misspelled it. The handy custom 404 error message that popped up suggested the correct spelling with a hyperlink to the image.

Being a tech geek, and dyslexic, I instantly wanted to add spell check functionality to my error pages. Alas, my hosting company did not turn mod_spelling on for shared hosting.

That didn’t mean my 404 pages couldn’t be smart, or at least smarter. I implemented a referrer check, to see if the 404 page was generated by an internal linking error, and to e-mail me so I could correct the problem. I also added the ability to break a URL up into a query string so someone could search for the intended page. This was all back in 2009, hence the links from bluecentauri (the precursor to SarahKTyler.com apps, the precursor to Datayze.com.)

For the past seven years my error pages have remained largely unchanged. I did modify the email code section to email me regardless of referrer. Referrer links is how I survive. If someone is posting a link to my apps, there’s a good bet it’s in a online forum, they like the app, and they think others in the forum will like the app as well. Those are valuable eyeballs and I want to make sure they get where they’re intending to go! About 1 time out of 5 they’ll be a typo in the URL link. I’m usually on it within 2 hours, thanks to the email alert. That’s still two hours of visitors encountering 404 messages, and possibly giving up before reaching the intended app.

It dawned on me this morning that I don’t need mod_speling. I could implement it’s functionality in PHP!

The basic functionality is really simple. Start with the page URL ($url = $_SERVER[‘REQUEST_URI’]), iterate through a list of my app pages (foreach($applist as $app_url)), and check if one is a substring of the other (if substr($url, $app_url) !== false || substr($app_url, $url) !== false). The extra equal sign is important, since substr can return a number, and zero evaluates to false. This catches the case where the URL copied by the poster is not the full URL, or there’s a trailing space or punctuation afterwards like an extra quotation mark. Next I checked the edit distance using the levenshtein function to catch misspellings, which calculates the number of keystrokes needed to convert one string into another.

So I did! And I wrote up a nice how to tutorial for Datayze.

I went back over my error logs and tested the new script on every externally linked URL that caused a 404 error. It corrected every single one.

Too bad I didn’t think of this much sooner.

Posted in Work Life | Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *