Blog/ How to theme 403 and 404 error pages?

Drupal allows us to add custom 403 and 404 pages.

Creating custom 403 and 404 pages

  1. Create two nodes, one for 403 and the other for 404.
  2. Navigate to "Configuration" -> "System" -> "Site Information" or admin/config/system/site-information.
  3. Add the paths of the pages into the appropriate fields.
  4. Now every time you visit 403 and 404 pages, you will be redirected to your custom pages.

Set custom 403 and 404 error pages in Drupal

Setting template suggestion

We can set "theme_hook_suggestions" variable to write our custom template for 403 and 404 pages. It is a special variable that can be set by a pre-processor function in our template.php.

Write the following piece of code in your template.php file or you can add the code in the existing hook_preprocess_HOOK() function.

function THEMENAME_preprocess_page(&$vars) {
  $header = drupal_get_http_header("status");
  if ($header == "404 Not Found") {
    $vars['theme_hook_suggestions'][] = 'page__404';
  }
  elseif ($header == "403 Forbidden") {
    $vars['theme_hook_suggestions'][] = 'page__403';
  }
}

Creating template files

Create two files in your theme directory:

  • page—404.tpl.php
  • page—403.tpl.php

Now, simply add your codes in these files and have fun!!

 
Ready to get started?REQUEST A QUOTE