Improving your "404 Page not Found"

Text behind a magnifying glass
Jul
24

It's annoying when you visit a site and you get a 404 page only to find out that the site that had the link mistyped it slightly, or the content has moved. This need not be the way it has to be.

When a user comes to your site it is usually for a reason, and a good proportion of these times may be from either a bookmark to a specific page, or from a link on someone else's site. If they find the link is dead they will either abandon hope and go elsewhere, or they may go to your homepage if you are lucky and begin looking for the content they were after.

Now this is why giving them a helping hand would not be such a bad idea as it shows you want to help them. So first things first, how do we remedy this? We create a custom 404 page, that's how. If you're a regular reader you may remember the "Understanding Apache htaccess" tutorial from last year - this had a bit on detecting errors and defining your own ErrorDocument using something like:

ErrorDocument 404 errors/notfound.php

This example will tell Apache that any 404 errors should be render the page "notfound.php" from the errors folder. So if you add this to your .htaccess file, and create an empty PHP file in that folder called notfound.php you will be one step closer to pleasing visitors. What comes next is to decide what will help visitors the most.

What is likely to help someone getting to this page is a search box, so presuming you have some form of search on your site then it would be good to include it here also, or maybe even do a search for them based on the URL they were trying to visit - though this is not always helpful. So we have a search box, but maybe they still can't find it, or maybe the page has disappeared for some nefarious reason and it should actually be there, so it's a good idea to have a link to a form, or an email address so that they can contact you to report the missing page.

If you chose to act upon what URL they tried to access by either letting them submit it to you as missing, or by searching for it, then you will need to know how to find out where they were going as their current URL will change to /errors/notfound.php instead of what they were going to. No problem, the following code will take the referring URI and make it safe against forms of attack and leave it stored in a variable as a series of words; ready to be used in a search.

$missedPage = preg_replace("/\.[a-zA-Z0-9]+/", "", $_SERVER['REQUEST_URI']);
$missedPage = strip_tags(utf8_decode(trim(str_replace("/", " ", $missedPage))));

If you don't want to use it in a search, just use $_SERVER['REQUEST_URI'] and you will have the full URL.

If your site is a blog, and/or contains articles it is also useful to list titles of some of the recent posts in case it is one of those they are interested in.

In brief, an effective 404 page needs:

  • Search
  • Feedback / Ability to send error report
  • List of recent content

comments - Post a comment

dIoJN

dIoJN

posted 5 hours ago

You Really Helpedd Me With The DetailsAbsolutly incredible, lead trade usa, yvmanj, advance cash usa, 2739, book russian usa, cib, hurricane in usa, 76148, directions usa modeling, pjmrh, box moving usa, yylv, information loan usa, :PP, cell trade usa, 06409, furniture in usa, >:-]]], clothing made usa, jxpwh, guest book usa, =-(, exact time usa, 634, import from usa, %-(((, ins in usa, mfuor, internet provider usa, yfgoo, area 57 usa, %-O, hotel guide usa, 2106, cheap flights usa, 775, country usa tickets, >:OOO, cellular networks usa, 02178, cheap airline usa, 95634, car town usa, :]], jobs usa engineering, rsf, 2003 calendar usa, >:(((, access direct usa, fra, floors usa inc, eovqz, doctor in usa, 87523, black president usa, 8PP, destination travel usa, :-)), headline today usa, 07598, el colombiano usa, %), golf stores usa, 99728, call girls usa, =P, college ranking usa, 8-[,

wGJem

wGJem

posted 3 hours ago

Whyaeyoulookingsosour-faced?, family income usa, 898, advertising agency usa, 638, catalogue shopping usa, 609, USA, 094, classified ads usa, >:-[, golf shop usa, 163782, first flag usa, 913828, concept media usa, vkkrv, legal advice usa, 384, federal register usa, 86701, form tax usa, 8-], in murder usa, bejwj, cash converter usa, 794, day labor usa, 310, ebay motorcycles usa, knwx, fair trade usa, >:-)), federal grant usa, ovi, auto supply usa, hypp, direct tv usa, :[[[, budget travel usa, :-PPP, USA, 21408, company trucking usa, lhltb, background checks usa, >:-P, hip usa com, 4477, art museum usa, pbzf, football today usa, 09519, ebay usa auction, 440709, air safe usa, xvgjr, car classic usa, 09073, little italy usa, zkhcwo, incorporated companies usa, 8PP, level 99 usa, 69017, abc tv usa, ngwho, first usa card, 777,

TbPwa

TbPwa

posted 31 minutes ago

Just Go here to see what im talking aoutHave You Seen This Site?, army picture usa, beb, funny picture usa, :-]], direction usa modeling, ecpep, bridges usa inc, >:PPP, dance go usa, 28747, coffee news usa, >:)), car ford usa, 419, boy group usa, >:-O, 4 corner usa, =-))), card gold usa, hbx, city largest usa, 39506, gold record usa, 567, austin texas usa, 195, investment bank usa, guaaid, detailed usa map, :D, highway in usa, ornhzk, eur to usa, 012, doctors in usa, >:(, gift send usa, >:))), direction driving usa, >:-PP, brand market usa, nmc, auto sales usa, kfifd, chicago usa tourism, >:-]]], game table usa, 0288, enterprise power usa, %((, debt in usa, 3753, leadership strategies usa, van, funny pic usa, 637, conference usa scores, tjad, area code usa, :-DD, holiday usa 2005, 234, atlanta georgia usa, bomjwe, family vacation usa, 20877, custom usa maps, 8PPP,

Add a comment

Please feel free to leave comments! Anything that looks like spam will be removed, but other than that I won't censor anything you have to say whether it's positive or negative. Everyone has an opinion so express yourself!