Actions

Difference between revisions of "Custom error pages"

From Joomla! Documentation

(HTTP Status Code: updated code for changed syntax)
(10 intermediate revisions by 9 users not shown)
Line 1: Line 1:
=== Custom error pages ===
+
Joomla! uses the ''templates/system/error.php'' file to handle several HTTP Status errors, including "403 Forbidden", "404 Not Found", and "500 Internal Server" errors. You can style the error results, if desired.
  
Joomla! uses the templates/system/error.php file to handle several HTTP Status errors, including "403 Forbidden", "404 Not Found", and "500 Internal Server" Errors. You can style the error results, if desired.
+
It is important to understand that ''error.php'' is an independent file from the Joomla! CMS but dependent on the Joomla! Platform. Plugins do not run on the file. You cannot include modules or use  ''<jdoc:include>'' statements.
 
+
It is important to understand that the error.php is an independent file from Joomla!. Plugins do not run on the file. You cannot include modules or use  ''<jdoc:include>'' statements.
+
 
+
==== Overriding the System Error Results ====
+
  
 +
====Overriding the System Error Results====
 
To override the system error results, copy the ''templates/system/error.php'' file into your ''templates/<template-name>'' directory.  
 
To override the system error results, copy the ''templates/system/error.php'' file into your ''templates/<template-name>'' directory.  
  
Line 13: Line 10:
 
You can format the page, as desired, to match your template.
 
You can format the page, as desired, to match your template.
  
==== Overriding the System Styling ====
+
====Overriding the System Styling====
 
+
 
If you want to change the styling, copy the ''templates/system/css/error.css'' file into your ''templates/<template-name>/css'' directory.  
 
If you want to change the styling, copy the ''templates/system/css/error.css'' file into your ''templates/<template-name>/css'' directory.  
  
Next, update the your ''templates/<template-name>/error.php'' file to reference the new location of the stylesheet by changing this line, accordingly:
+
Next, update your ''templates/<template-name>/error.php'' file to reference the new location of the stylesheet by changing this line, accordingly:
 
<source lang="css"><link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error.css" type="text/css" /></source>
 
<source lang="css"><link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error.css" type="text/css" /></source>
  
Then, simply change the error.css, as desired, for your styling requirements.
+
Then, simply change the ''error.css'', as desired, for your styling requirements.
 
+
==== Customizing Error Messages ====
+
  
 +
====Customizing Error Messages====
 
You can add conditional logic to vary the message returned, dependent upon the specific error code.  
 
You can add conditional logic to vary the message returned, dependent upon the specific error code.  
  
 
Here is an example of how to trap a 404 error and provide a custom message.
 
Here is an example of how to trap a 404 error and provide a custom message.
 
<source lang="php">
 
<source lang="php">
<?php if ($this->error->code = '404') { ?>
+
<?php if ($this->error->getcode() == '404') { ?>
 
<div id="errorboxheader">Page not found</div>
 
<div id="errorboxheader">Page not found</div>
 
<div id="errorboxbody"><p>Sorry! That page cannot be found.</p>
 
<div id="errorboxbody"><p>Sorry! That page cannot be found.</p>
Line 36: Line 31:
 
</source>
 
</source>
  
==== HTTP status code ====
+
====HTTP Status Code====
 
+
 
When a request is made for a page on your site, the server returns an HTTP status code in response to the request. Joomla! returns a '200 - the server successfully returned the page' for error pages. This is problematic for those working with Google Webmaster Services and trying to get a sitemap resolved.
 
When a request is made for a page on your site, the server returns an HTTP status code in response to the request. Joomla! returns a '200 - the server successfully returned the page' for error pages. This is problematic for those working with Google Webmaster Services and trying to get a sitemap resolved.
  
Line 43: Line 37:
 
<source lang="php">
 
<source lang="php">
 
<?php  
 
<?php  
if ($this->error->code = '404') {
+
if ($this->error->getcode() == '404') {
 
header("HTTP/1.0 404 Not Found");
 
header("HTTP/1.0 404 Not Found");
 
} ?>
 
} ?>
 
</source>
 
</source>
  
'''More HTTP status code information:'''
+
====More HTTP Status Code Information====
  
 
* [http://docs.joomla.org/Tutorial:Create_a_Custom_404_Error_Page Joomla! Documentation: Create a Custom 404 Error Page]
 
* [http://docs.joomla.org/Tutorial:Create_a_Custom_404_Error_Page Joomla! Documentation: Create a Custom 404 Error Page]
 
* [http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html HTTP/1.1 Status Code Definitions]
 
* [http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html HTTP/1.1 Status Code Definitions]
 
* [http://www.google.com/support/webmasters/bin/answer.py?answer=83040&topic=8474 Google Webmaster - Analyzing Crawl Errors]
 
* [http://www.google.com/support/webmasters/bin/answer.py?answer=83040&topic=8474 Google Webmaster - Analyzing Crawl Errors]
 +
 +
====Using Theme Header and Footer on Standard Error Page====
 +
{{JVer|1.5}}
 +
If you want to see the error page in theme design and don't like redirecting to error page URL or duplicating HTML in the error page template, here is a way to apply your theme template to the error page.
 +
 +
First, put the following code in ''templates/<template-name>/error.php'':
 +
 +
<source lang="php">
 +
<?php
 +
// no direct access
 +
defined( '_JEXEC' ) or die( 'Restricted access' );
 +
 +
include dirname(__FILE__) . "/index.php";
 +
?>
 +
 +
</source>
 +
 +
Then make the following edits to ''templates/<template-name>/index.php''::
 +
 +
1. Find the following code in ''index.php''
 +
 +
<source lang="php">
 +
<jdoc:include type="head" />
 +
</source>
 +
 +
and replace it with the following
 +
 +
<source lang="php">
 +
<?php if (!$this->error->code) : ?>
 +
<jdoc:include type="head" />
 +
<?php else : ?>
 +
<title><?php echo $this->error->code ?> - <?php echo $this->title; ?></title>
 +
<?php endif; ?> 
 +
</source>
 +
 +
2. Find the following code in ''index.php''
 +
 +
<source lang="php">
 +
<jdoc:include type="component" />
 +
</source>
 +
 +
and replace it with the following
 +
 +
<source lang="php">
 +
<?php if ($this->error->code) : /* check if we are on error page, if yes - display error message */ ?>
 +
<p><strong><?php echo $this->error->code ?> - <?php echo $this->error->message ?></strong></p>
 +
 +
<p><strong><?php echo JText::_('You may not be able to visit this page because of:'); ?></strong></p>
 +
<ol>
 +
<li><?php echo JText::_('An out-of-date bookmark/favourite'); ?></li>
 +
<li><?php echo JText::_('A search engine that has an out-of-date listing for this site'); ?></li>
 +
<li><?php echo JText::_('A mis-typed address'); ?></li>
 +
<li><?php echo JText::_('You have no access to this page'); ?></li>
 +
<li><?php echo JText::_('The requested resource was not found'); ?></li>
 +
<li><?php echo JText::_('An error has occurred while processing your request.'); ?></li>
 +
</ol>
 +
<p><strong><?php echo JText::_('Please try one of the following pages:'); ?></strong></p>
 +
<p>
 +
<ul>
 +
<li><a href="<?php echo $this->baseurl; ?>/index.php" title="<?php echo JText::_('Go to the home page'); ?>"><?php echo JText::_('Home Page'); ?></a></li>
 +
</ul>
 +
</p>
 +
<p><?php echo JText::_('If difficulties persist, please contact the system administrator of this site.'); ?></p> 
 +
<?php else : ?> 
 +
<jdoc:include type="component" />
 +
<?php endif; ?> 
 +
</source>
 +
 +
Now your theme template is applied to error pages too.
 +
 +
'''Note:''' Module includes in template will not work on error page created by this method (but will work on other pages).
 +
 +
[[Category:Tutorials]][[Category:Template Development]]

Revision as of 06:27, 12 March 2014

Joomla! uses the templates/system/error.php file to handle several HTTP Status errors, including "403 Forbidden", "404 Not Found", and "500 Internal Server" errors. You can style the error results, if desired.

It is important to understand that error.php is an independent file from the Joomla! CMS but dependent on the Joomla! Platform. Plugins do not run on the file. You cannot include modules or use <jdoc:include> statements.

Contents

Overriding the System Error Results

To override the system error results, copy the templates/system/error.php file into your templates/<template-name> directory.

If it finds one, Joomla! will use the error.php file from the current template, in place of the system file.

You can format the page, as desired, to match your template.

Overriding the System Styling

If you want to change the styling, copy the templates/system/css/error.css file into your templates/<template-name>/css directory.

Next, update your templates/<template-name>/error.php file to reference the new location of the stylesheet by changing this line, accordingly:

<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error.css" type="text/css" />

Then, simply change the error.css, as desired, for your styling requirements.

Customizing Error Messages

You can add conditional logic to vary the message returned, dependent upon the specific error code.

Here is an example of how to trap a 404 error and provide a custom message.

<?php  if ($this->error->getcode() == '404') { ?>
        <div id="errorboxheader">Page not found</div>
                <div id="errorboxbody"><p>Sorry! That page cannot be found.</p>
                </div>
        </div>
<?php } ?>

HTTP Status Code

When a request is made for a page on your site, the server returns an HTTP status code in response to the request. Joomla! returns a '200 - the server successfully returned the page' for error pages. This is problematic for those working with Google Webmaster Services and trying to get a sitemap resolved.

If you want Joomla! to return a status code for the error, you can do so by adding logic before the DOCTYPE line, as follows:

<?php 
if ($this->error->getcode() == '404') {
        header("HTTP/1.0 404 Not Found");
} ?>

More HTTP Status Code Information

Using Theme Header and Footer on Standard Error Page

Joomla 1.5 If you want to see the error page in theme design and don't like redirecting to error page URL or duplicating HTML in the error page template, here is a way to apply your theme template to the error page.

First, put the following code in templates/<template-name>/error.php:

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
include dirname(__FILE__) . "/index.php";
?>

Then make the following edits to templates/<template-name>/index.php::

1. Find the following code in index.php

<jdoc:include type="head" />

and replace it with the following

<?php if (!$this->error->code) : ?>
<jdoc:include type="head" />
<?php else : ?> 
<title><?php echo $this->error->code ?> - <?php echo $this->title; ?></title>
<?php endif; ?>

2. Find the following code in index.php

<jdoc:include type="component" />

and replace it with the following

<?php if ($this->error->code) : /* check if we are on error page, if yes - display error message */ ?>
<p><strong><?php echo $this->error->code ?> - <?php echo $this->error->message ?></strong></p>
 
<p><strong><?php echo JText::_('You may not be able to visit this page because of:'); ?></strong></p>
<ol>
<li><?php echo JText::_('An out-of-date bookmark/favourite'); ?></li>
<li><?php echo JText::_('A search engine that has an out-of-date listing for this site'); ?></li>
<li><?php echo JText::_('A mis-typed address'); ?></li>
<li><?php echo JText::_('You have no access to this page'); ?></li>
<li><?php echo JText::_('The requested resource was not found'); ?></li>
<li><?php echo JText::_('An error has occurred while processing your request.'); ?></li>
</ol>
<p><strong><?php echo JText::_('Please try one of the following pages:'); ?></strong></p>
<p>
<ul>
<li><a href="<?php echo $this->baseurl; ?>/index.php" title="<?php echo JText::_('Go to the home page'); ?>"><?php echo JText::_('Home Page'); ?></a></li>
</ul>
</p>
<p><?php echo JText::_('If difficulties persist, please contact the system administrator of this site.'); ?></p>  
<?php else : ?>  
<jdoc:include type="component" />
<?php endif; ?>

Now your theme template is applied to error pages too.

Note: Module includes in template will not work on error page created by this method (but will work on other pages).