A quick noob question here! How to set a theme to use a custom template for 404 error pages instead of using page.tpl ?
5 Answers
Follow these steps:
Make a new page using the Panels module (Empty page or put in it any content)
In admin/config/system/site-information set Default 404 (not found) page to your new page
Override the template file for that page (page--error.tpl.php)
Remove all the regions you don't need and put your code
-
5And one does not even need to use panels. Module handling a page or simplest "Basic page" node would do, too.– MołotOct 29, 2013 at 8:55
-
2
-
1The 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.– roobyMar 20, 2018 at 1:00
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. Mar 10, 2016 at 17:20 -
-
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) ?– MatoeilMay 11, 2017 at 10:29
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.– evanmcdJan 29, 2014 at 1:34
-
1We 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.– alexkbJan 29, 2014 at 3:28
These days you get a page--404.html.tpl
and a page--4xx.html.tpl
theme suggestion out of the box.
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 :
- Create a basic base and call it as you want.
- Assign that page to the 404 response admin/config/system/site-information
- Enable the theme_debug mode.
- Type a stupid url in your address bar.
- You arrive on your page initially created
Look at the source and the templates suggestion, you will get something like :
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.– roobyMar 20, 2018 at 1:01