Creating custom error pages in Drupal

<p>Sorry, no content was found at the requested path - it's possible that you've requested this page in error.</p>

<p>Use the search form below, or go to the <a href="/">home page.</a></p>

<?php
// check that the search module exists and the user has permission to hit the form
if (module_exists('search') && user_access('search content')) {
 
// cool! - customerror doesn't trash the page request and the full path is available
 
$path = $_REQUEST['destination'];
 
// bin anything that's not alphanumeric and replace with spaces
 
$keys = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', ' ', $path));

  // retrieve the search form using the data we've pull from the request
  // note that we can override the label for the search terms field here too
 
print drupal_get_form('search_form', NULL, $keys, 'node', 'Search terms');
}
?>

<?php global $user; ?>
<?php if ($user->uid): ?> 
  <p>Sorry <?php print $user->name; ?>, you don't have permission to view the page you've just tried to access.</p>
  <p>If you feel that you have received this message in error, please
    <a href="contact">contact us</a> with specific details so that we may review your access to this web site.</p>
  <p>Thanks</p>
<?php else: ?>
  <p>This page may be available to clients and registered users only. Please select from one of the other options available to you below.</p>
  <ul>
    <li><a href="/user/login?<?php print drupal_get_destination(); ?>">Login</a> to view this page</li>
    <li>Use the <a href="search">search</a> facility</li>
    <li>Go to the <a href="/">home page</a></li>
    <li>Go to the <a href="sitemap">site map</a></li>
  </ul>
<?php //print drupal_get_form('user_login'); ?>
<?php endif; ?>

Blog Tags: DrupalModulesUsabilityPHP

Comments

Great post, i've been using search404 up to now, but Customerror module seems worth checking out!

Thanks

So I used modified versions of these snippets on a site I'm working on and they work great. One caveat though...

Since the customerror module uses some custom callbacks to execute its code, and those callbacks aren't in the menu system, breadcrumbs and navigation will possibly change on the 403 snippet. My client noticed that the although the URL was the same, all the highlighted menus and blocks went away. This one line snippet fixed all that:

<?php menu_set_active_item($_REQUEST['destination']); ?>

All it does is trick Drupal into thinking the the requested URL is the page it's on even when a different callback is really handling the request.

I have found none. To the best of my knowledge it only affects the menu system, particularly highlighting of menu items in the active trail. And while the menu system does control access to many pages, it is still smart enough to do all access checks against $user, which is never changed.

I added the call to the 404 snippet and it stopped any menus from appearing - I just now noticed you were adding it to the 403 snippet not 404 doh!

What would i do with errors other than 404 and 403. I have other databases on my website and the codes are in drupal pages. How do i deal with errors of database not found when i issue sql or specific sql errors each having its own type of safe error page.
Thanks

Great post/information! Thank you...

I have implemented this in v6 and I lose all the menus and blocks... If I navigate to www.mysite.com/customerror/403 I get all my nav and blocks and the error page BUT if I let Drupal show the error page, the URL does not rewrite form the erroneous one and I lose all my nav and blocks... I tried that code above to no avail (I pasted it in the code blocks)... Any ideas?

Good analysis, Nik, thanks.

Having a db search query on all 404's was a major consideration for me, and something I decided to try, and evaluate.

Providing the search form without performing the search is probably a much better solution -- and an option that could maybe be included in Search 404.

A few other considerations:


For instance, [re: Search 404] the node “/blog/my-drupal-article” will almost certainly contain all the words “my drupal article”, but may not contain the word “blog”, except in the path. This means the search doesn’t catch that article, so you get no results.

Search 404 provides Advanced Settings on its config page where you can make a list of words to ignore -- it includes "and or the" by default, and I've added terms like "blog node content" that show up in my paths, but aren't appropriate for searches.

Another benefit, for me, is that Search 404 allows for easy inclusion of sidebar blocks on the 404/403 pages. (Yes, I've read #129762.) I think the performance hit is worth keeping the blocks there ~ especially since Search 404 apparently only displays cached blocks.

I guess I either want sidebar blocks display in CustomError, or an option to not perform the search in Search 404...

Sorry Nik, I should've read the comments more thoroughly.

You say Blocks404 works for D5 and D6, but there's only a D6 version on the project page. Unless you know something I don't, I'll look into backporting when I have a chance...

Whew, backbreaking. I finally got everything working according to your instructions with d6. I did some modifications to the 404 page. (I used customerror and 404blocks as suggested btw). But I still wasn't able to get the "bad url" to go into the google CSE results box. Ideally I wanted it to go in there with the results also already appearing. Maybe next time I can figure it out, or if anyone else knows how to do it please let me know. http://philippines-living.com/bar3 and http://central-america-forum.com/bar3 are URLs to nonexistent files on the two sites I serve off that d6 codebase if you want to take a look. Thanks for providing your description and tips here.

All this works great for me (Drupal 5.15). Thanks for this advice.

The only problem is that I get ugly html-errors because of duplicate Ids by using:

print drupal_get_form('search_form', NULL, $keys, 'node', 'Search terms');

in my 403/404 pages. Is there anything I can do to avoid this?

Thanks for the advice. I switched from Search404.

Awesome tutorial, definitely saved me an hour or 2.

THANK YOU!

Hi,

First, I am using Drupal 6.x and I have loaded into customerror module 6.x within /drupal/sites/all/modules, however I am not seeing Custom Error module’s config page at “/admin/settings/customerror”.

I am seeing "/admin/settings" instead.

What could be wrong with my setup?

Second, the two code samples in this document's section "Customerror module", where are they applied?

Thanks for this I think it adds a lot to the useability of our site and it saved me some time figuring it out myself.

However I cam across a problem:

While logged out I went to /admin and got the 403 page in the admin theme (Garland), I entered correct username/password into logon box in the left hand column and got sent to 403 page again but now in the custom front end theme.

If I clicked on the log in link on the 403 page and entered correct username/password I was logged in and redirected to /admin.

I had copy'n'pasted the 403 snippet exactly as above and I fixed it by adding in the line:

<?php menu_set_active_item($_REQUEST['destination']); ?>

as suggested by Michael Prasuhn above (not that I thought it would help but I wanted that functionality but had mistakenly read his comment as applying to 404's not 403's and had just noticed and changed it).

Thanks a lot. I've had the same experience with Search404. I will try Cutomerror, maybe adding the google code for 404 pages.
Cool article. Thanks

This is an excellent resource, Nik, and I'm generally using this approach, now, in lieu of Search 404.

I wonder if you might prepend appropriate links in your examples with <?php print $GLOBALS['base_path']; ?> so that they work on sites that aren't at the root dir of a domain... Might help folks that are copy-pasting.

Thanks again!

Many thanks for an excellent solution!

thankyou, thankyou, thankyou!!

I absolutely agree with you on this. Search404 is great conceptually, but produces a LOT of overhead, and this solution is an elegant alternative. It's funny that Search404 is catching on so much--it's even mentioned as a 'to do' in the 'SEOchecklist' module. I think in most cases it's a module that should just be skipped.

Any idea how to pre-populate just a search box (rather than the whole form)?

I blindly used:

print drupal_get_form('search_box', NULL, $keys, 'node', 'Search terms');

Which gave me the text: 'Search this site', a text input box and a 'Search' input button - which is dead easy to theme...

But I've no idea how the form API works, or what arguments I should really be passing. I've looked at api.drupal.org and it's left me none the wiser. Grateful for any help

Wow thanks, this post and thread were very valuable. I finally have a witty 404, search, and my site frame.

Great tutorial, thanks a lot

Thank you, this post was really helpful.

Many thanks. Great and simple post to implement successfully for Drupal newbei also.
Cheers

Add new comment