12

A quick noob question here! How to set a theme to use a custom template for 404 error pages instead of using page.tpl ?

10

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

  • 5
    And one does not even need to use panels. Module handling a page or simplest "Basic page" node would do, too. – Mołot Oct 29 '13 at 8:55
  • 2
    Why would you need a contributed module to create a 404? – MrUpsidown Aug 19 '15 at 6:36
  • 1
    The problem with using a node, such as a basic page, is that it will then come up in search results and other content related listings, which is undesirable. – rooby Mar 20 '18 at 1:00
27

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.

  • Remember to clear your cache if you just created the page--404.tpl.php file. – Blake Erickson Mar 10 '16 at 17:20
  • For 403, the $header == '403 Forbidden'. – Henry Aug 24 '16 at 20:42
  • this does not seem to work in a module, the template in the module folder is not overtaken. how to do the same with function YOURMODULE_preprocess_page(&$vars) ? – Matoeil May 11 '17 at 10:29
3

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.

  • That looked good, but for those of you also using the redirect module it seems that they don't play well together. I could never get customerror to show it's page over what redirect showed. – evanmcd Jan 29 '14 at 1:34
  • 1
    We use Redirect and Customerror modules in unison without these issues. Looking over the settings pages of both modules, it looks like customerror (in addition to its custom error functionality) also has a feature for managing redirects, albeit with a crude interface. If you are using redirect (and the OP isn't), then simply avoid using the customerror redirect settings, and you shouldn't have any issues. – alexkb Jan 29 '14 at 3:28
0

Without assuming any convention which may exist in your theme base, the question is finally how can I get to know for sure what template file to use ...

Here are the steps :

  1. Create a basic base and call it as you want.
  2. Assign that page to the 404 response admin/config/system/site-information
  3. Enable the theme_debug mode.
  4. Type a stupid url in your address bar.
  5. You arrive on your page initially created
  6. Look at the source and the templates suggestion, you will get something like :enter image description here

  7. That's straight forward, no preprocess file or template.php to modify.

Myself I have choseen the one marked by a x, as I wanted something naked with afull frame pic and just a link to back.

  • Note that the basic page you created for the 404 error will also now appear in search results as a content page. – rooby Mar 20 '18 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.