Knowledgebase

How to set and change the default error pages in Drupal

Drupal comes preconfigured with some default generic error pages. These include the Not Found and Access Denied pages. For example, when a user requests a page that doesn't exist on your site, the generic Not Found (HTTP error code 404) page will be displayed; its title is Page not found and the text underneath is The requested page "/path-to-the-page" could not be found. In case with the Access Denied error (HTTP error code 403), the title of the default generic page is Access denied and the content is You are not authorized to access this page. If you want to, you can change the default error pages. There are a few ways you can do this.

One way to do this is to create a node/page from the admin panel of your Drupal and then set it to be the default page that's displayed for the particular error. To do this, after you create the node click on the Configuration tab in the top navigation bar of your admin panel and on the page that opens click on the Site information button. On the following page scroll down to the bottom where you'll see a section labeled Error Pages. In that section there are a couple of text fields. One is for the default Not Found page and the other for the default Access Denied page.

When these fields are empty the generic pages are shown. To make the new node that you created the default page for the particular error simply type its path in the respective text field. You only need to type the local path that's added to the base URL. For example, if the full URL address of the node that you created is yourdomain.com/node/31, then you only need to type node/31 in the field. When you create the node for the error page it's a good idea to disable the comments for that node and not to promote it to front page. It's also a good idea to use a content type that is set not to display the name of the author and date of publication.

Another way to set a custom error page is to directly change the generic error messages that come preconfigured with Drupal. You can do this by either modifying the file that contains these messages or by using a contributed module to override the messages/strings. The error messages are contained in a file called common.inc. If your Drupal is installed directly in the public_html folder on your Drupal hosting account, meaning the site is accessible at yourdomain.com, then the path to the file on your account will be public_html/includes/common.inc. One way to modify it is from the Files section of the Pixie control panel. For the Not Found error page you need the code:

// Standard 404 handler.
 drupal_set_title(t('Page not found'));
 $return = t('The requested page "@path" could not be found.', 
array('@path' => request_uri()));

Just change the relevant text for the title and the content to whatever you want. This is the text that's enclosed in single quotation marks. The part @path will display the local path of requested page.

For the Access Denied error page the text is in the same common.inc file. It's in the following piece of code:

// Standard 403 handler.
 drupal_set_title(t('Access denied'));
 $return = t('You are not authorized to access this page.');

Note that it's not recommended to change the code of core Drupal files. These changes will be overwritten when you upgrade Drupal. A way to avoid this is to use a contributed module called String Overrides. You can use it to replace the above mentioned text in the common.inc file (and strings in other files). You can do that from the admin panel without modifying the file itself, and upgrading Drupal will not overwrite any changes made through the module.

Was this answer helpful?

 Print this Article

Also Read