Drupal - Custom template for 404 error pages

Yet another solution from this discussion that works without panels or other custom modules.

In your theme folder locate template.php and write this code. Replace YOURTHEME with the name of your theme:

function YOURTHEME_preprocess_page(&$vars) {
  $header = drupal_get_http_header('status'); 
  if ($header == '404 Not Found') {     
    $vars['theme_hook_suggestions'][] = 'page__404';
  }
}

Whenever 404 occurs your page--404.tpl.php will be used. Remember to put "--" not "__" in your template file name.


Follow these steps:

  1. Make a new page using the Panels module (Empty page or put in it any content)

  2. In admin/config/system/site-information set Default 404 (not found) page to your new page

  3. Override the template file for that page (page--error.tpl.php)

  4. Remove all the regions you don't need and put your code


If you want to have a customized 404 page that doesn't involve creating a page (node), use the customerror module.

If you have a 404 page in Drupal, then it's going to show up in search results, and/or your Views listings.

When using the customerror module, you can override the template by creating a page--customerror.tpl.php in your theme folder. Remember also to flush your cache after you've created new template file.

Tags:

Theming