Drupal: lean page template for 403 and 404 errors

Drupal

In order to reduce the server load and data sent on a 404 (not found) or a 403 (access denied) I prefer to serve a slimmed-down page without any blocks, etc. I copied page.tpl.php as page--error.tpl.php and removed most of the regions/blocks, breadcrumbs, menus, etc.

Then I used drupal_get_http_header() to obtain the HTTP status in the page preprocess function to detect when we are encountering a 404 or 403. With theme_hook_suggestions we can suggest a page template for Drupal.

<?php
function MYTHEME_preprocess_page(&$vars) {

 
/* Create theme suggestions for 403 and 404 errors
  Values returned: '403 Forbidden' or '404 Not Found' */
 
$status = drupal_get_http_header("status");
  if (
$status == '404 Not Found' || $status == '403 Forbidden') {
   
$vars['theme_hook_suggestions'][] = 'page__error';
  }

}
?>

This approach still uses the nodes defined via /admin/config/system/site-information.

An improvement over an earlier attempt and much easier than modifying each block individually. Plus, I have blocks that determine the visibility based on the outcome of PHP code so the option to exclude it from pages is not available without more coding. This approach integrates nicely with the site theme.