« Random Entries Using PHP | Main | PHP and MySQL for Dynamic Web Sites - Book Review »

Custom Error Messages

Updated. Originally published Jan 2, 2004

Error messages are the messages that are displayed when a visitor to your site encounters a server error. The most common error messages are 404: File Not Found and 403: Access Forbidden. The 404 error message is displayed when a visitor to your site tries to access a page that does not exist on your server. The 403 message is displayed when a visitor tries to enter an area on your site that is off limits to the public.

You can customize your error messages so that instead of seeing an ugly server message, your visitor sees instead a personalized note from you. The methods are different depending on if you are doing static (the default) or dynamic publishing in Movable Type.

Static Publishing Error Templates

If you use an Apache server you can customize your error messages by creating or editing your .htaccess file. Here are the steps:

Step 1: Create 403 and 404 error html documents:

Create new html files with the error messages that you want to convey for 403 errors and 404 errors. Save the files as 403.html and 404.html.

You can word the contents of these documents however you please. Place the html documents in your public_html directory.

Step 2: Change or create your .htaccess file.

Inside your public_html directory, or one of its subdirectories, you may have a file named .htaccess (see What is .htaccess?). If an .htaccess file isn't already there, create a simple text file named .htaccess in a text editor. Now add the following lines to your .htaccess file.

ErrorDocument 403 PATH
ErrorDocument 404 PATH

Make sure to replace where it says PATH with the path to that error message.

For example, your .htaccess file may look something like this if you have uploaded the html files and the .htaccess file to the same directory:

ErrorDocument 403 /403.html
ErrorDocument 404 /404.html

Step 3: Upload your new .htaccess file to your server using an ftp program.

Then test it out! This method can also be used with the other error codes: 400, 401, and 500.

Dynamic Publishing Error Templates

If you use dynamic publishing, Movable Type will point to the Dynamic Pages Error Template in your blog if a page can't be found. The default template body of this template is:

<p>The requested page could not be found.</p>
<h4 class="error-message"><$MTErrorMessage$></h4>

At the time of this writing, the MT Manual specifies that the the $MTErrorMessage$ tag can only be used in the Comment Error Template. The tag is replaced with the text of the error that occurred in the comment submission process. However, this tag also appears in the default Dynamic Pages Error Template. So, I'm assuming that if there is a system generated specific error message, the $MTErrorMessage$ tag will produce it. (Sometimes the MT Manual is a bit out of date.) You can change the message that comes between the p tags and replace The requested page could not be found. with something else.

In MT 3.12 the tag <MTHTTPErrorCode> was introduced. The tag only works with dynamic pages and can be used with a little PHP to generate different error messages for different error codes. Arvind has documented how to use the MTHTTPErrorCode tag to customize error messages in his Movalog tutorial - Custom Error Messages.

Additional Information

404 Redirects

I recently changed all of the file extensions on one of my blogs from html to php. Scripty Goddess wrote a 404 redirect PHP script that automatically redirects visitors to the .php file when they try to access the .html file that is no longer there. This script works if the URL paths are the same except for the html and php extensions.

Links:


Cool custom error messages:


Have one to recommend? Let me know in the comments!

Comments (5)

Elise,
Thanks for this! I'm trying several of your mods on my blog (all errors are mine), and find your site THE reference for how to do it with Movable Type!

BTW, I tried the above, adding Scripty-Godesses' php to modify the defauly mtview.php that's called in my .htaccess files, and it did something odd: MT seems to over-write them to their baseline (dull) default every day.

All is not lost, I just made new 404.php (and 403.php), and directed the .htaccess to them.

Just an FYI, and a big Thanks!

Paul Author Profile Page:

Elise,

I've used your LMT every day for the past 2 weeks and I am happy to say my first postings are going smoothly. Many, many thanks.
Do you know of a way to create personalized 403/404 files when running from a Windows server? Any references would be great.

Thanks,
Paul

p.s. I tried to trackback to one of your articles as I referenced you in a initial entry of mine..don't think it worked but I'm trying.

http://www.speth.tv/notebook/archives/individual/Movable%20Type%20madness.php

I'm sort of fond of this error message on my site.

http://artfulwriter.com/404.html

C.

I like kottke's 404 page.

http://www.kottke.org/404.html

kgs Author Profile Page:

Elise, my custom 404 redirect wasn't working, and I found out that my ISP, Dreamhost, by default, executes php by cgi. Without attempting to explain that, it means custom error messages won't work on Dreamhost unless you elect to run php as an Apache module, which I can't say I'd feel safe doing with my lack of expertise. I can't get ANY redirects functioning, however, which is another issue to deal with!

Post a comment

(If you haven't left a comment here before, your comment may need to be approved before will appear on the entry. Thanks for waiting.)