Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

User-Friendly 404 Page

The willmaster.com 404 page was updated recently to be more user friendly. The 404 page suggests certain destinations depending on which subdirectory the person tried to get content from.

I'll describe how we did it.

The code to suggest certain URLs is written in PHP. Thus, the 404 page must be a PHP web page.

In essence, the functionality uses the $_SERVER["REQUEST_URI"] variable to determine in which directory the failed page was requested from. If the directory is provided for in the code, a suggestion is published on the page.

In pseudo-code, it looks like this:

if directory=DirectoryName
   publish suggestion
else if directory=DifferentDirectoryName
   publish this suggestion
else if directory=StillAnotherDirectoryName
   publish this particular suggestion
end if

Specifying a Custom 404 Page Location

If your 404 page is in a custom location, traffic can be redirected to it by adding this line to the document root .htaccess file:

ErrorDocument 404 /custom404.php

Change the location colored blue in the above code to the location of your 404 page.

How to Make the User-Friendly 404 Page

Ensure your 404 page can run PHP code. Generally, that would be a page with a .php file name extension.

Here's an example of PHP code that can be customized for your implementation.

if( strpos($_SERVER["REQUEST_URI"],"/DirectoryName/") === 0 )
{
   echo "Please see <a href='http://example.com/OtherPage.php'>this page</a> for information you might be looking for.";
}

Replace the blue colored directory location with the directory location you want to spot. And replace the red colored link URL with the URL of the suggested page.

Repeat the above code block for each directory to spot, customizing the directory and it's corresponding suggested page. When repeated, the first word of the code block, "if", should be changed to "elseif". Only the first code block should have "if". The second and subsequent code blocks should have "elseif". (It's not strictly necessary to change "if" to "elseif", but it can make the processing of the page some clock ticks faster.)

Here's an example extracted from the willmaster.com custom 404 page.

if( strpos($_SERVER["REQUEST_URI"],"/DealsForYou/") === 0 )
{
   echo "A list of the latest and previous 'Deals for You' is <a href='//www.willmaster.com/DealsForYou/previousindex.php'>at this index page.</a>";
}
elseif( strpos($_SERVER["REQUEST_URI"],"/blog/") === 0 )
{
   echo "Please see <a href='//www.willmaster.com/blog/'>the blog index page</a> for a list of blog posts. (The <a href='//www.willmaster.com/library/'>library</a> may also have information you can use.)";
}
elseif( strpos($_SERVER["REQUEST_URI"],"/possibilities/") === 0 )
{
   echo "The <a href='//www.willmaster.com/possibilities/'>possibilities page</a> contains the latest issue.";
}

The number of blocks of PHP code you use is limited only by the number of directories you want to spot and publish suggestions for.

Here's an incentive for making a custom 404 page: A user-friendly 404 page may keep people interested rather than disappointed.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC