1

How can I alter the HTML output of the default 403 and 404 error messages without hacking core?

I know I can create custom pages via the UI, but this doesn't work for me because I need to deny access to all published pages to anonymous users.

Notes:

  • I believe the default 403 and 404 messages come from lines 2624 and 2653 on common.inc. This is what is generating my output at the moment - I assume. I don't think I can use a node for my output instead because anon users don't have permission to view published content.
  • I wanted to alter/override the default 403/404 messages outputed by common.inc. I can't redirect to a node because anon users don't have permission to view nodes.
  • 2
    possible duplicate of Custom template for 404 error pages – Mołot Oct 29 '13 at 7:56
  • 1
    Please use the above link that Molot has shared, that will solve your need. If that doesn't solve your requirement for some reason or the other, please let us know. – Gladiator Oct 29 '13 at 8:15
  • 3
    "I know I can create custom pages via the UI... this doesn't work for me because I need to deny access to all published pages to anonymous users." - sounds like a standard 403 page from here, nothing special about that. Just configure your 403 page and remove the 'access content' permission from anon. – David Thomas Oct 29 '13 at 10:20
  • 1
    We should wait to see what @user1919784 says, but this doesn't sound like a dup of the linked question. It could use a bit of a rewrite to clarify the point, but there are at least two good methods for denying anon access that could have proper answers. – mpdonadio Oct 29 '13 at 13:21
  • I believe the default 403 and 404 messages come from lines 2624 and 2653 on common.inc. This is what is generating my output at the moment - I assume. I don't think I can use a node for my output instead because anon users don't have permission to view published content. There seem to be two possibilities: page--error.tpl.php from the link and znerol's solution. Maybe my question is dumb because I'm missing something obvious? – dbj44 Oct 29 '13 at 15:54
2

Implement a custom module providing the 403/404 markup as a page callback. Note that access can be granted to anybody by setting the access callback in the menu router items to TRUE.

After enabling the custom module, you simply set the error pages in Configuration » System » Site information to the paths you defined in your hook_menu implementation.

/**
 * Implements hook_menu().
 */
function acme_menu() {
  $items['acme_404'] = array(
    'page callback' => 'acme_404',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['acme_403'] = array(
    'page callback' => 'acme_403',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Page callback for custom page not found.
 */
function acme_404() {
  return filter_xss(variable_get('acme_404_markup', 'custom 404 markup'));
}

/**
 * Page callback for custom access denied page.
 */
function acme_403() {
  return filter_xss(variable_get('acme_403_markup', 'custom 403 markup'));
}
|improve this answer|||||
  • Thanks. Why do you use variable_get in the callback functions? – dbj44 Oct 29 '13 at 23:06
  • Especially 404 messages tend to evolve over time due to changing customer needs. I tend to make public facing HTML configurable. However this reminds me that those fragments also should be passed at least through the xss filter. – znerol Oct 30 '13 at 7:49
0

Other solution is create 2 pages for 403 and 404 message error. Apply your themming with your CSS.

Then, go to "Site Information" (admin/config/system/site-information) and put the URL for your custom pages errors.

|improve this answer|||||
0

The CustomError module allows the site admin to create custom error pages for HTTP status codes 403 (access denied) and 404 (not found), without the need to create nodes for each of them. Some more details about its features (from its project page):

  • Configurable page title and descriptions.
  • There are no author and date/time headers as with normal nodes.
  • Any HTML formatted text can be be put in the page body.
  • The error pages are themable.
  • Users who are not logged in and try to access an area that requires login will be redirected to the page they were trying to access after they login.
  • Allows custom redirects for 404s.
|improve this answer|||||

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.