Difference between revisions of "Custom error pages"

From Joomla! Documentation

m (1 revision(s))
Line 1: Line 1:
 
=== Custom error pages ===
 
=== Custom error pages ===
The standard system error pages can be overriden by adding specially-named and constructed files to the template directory.  For example, this could be used to style the error pages so that they match the style of regular pages for the template.
 
  
Placing one or more of the following files into the templates/<template-name> directory will override the similarly named file in the templates/system directory.
+
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.  
  
* templates/<template-name>/403.php (HTTP Status code: 403 Forbidden)
+
==== Overriding the System Error Results ====
* templates/<template-name>/404.php (HTTP Status code: 404 Not Found)
 
* templates/<template-name>/500.php (HTTP Status code: 500 Internal Server Error)
 
* templates/<template-name>/component.php (not sure when this is used)
 
* templates/<template-name>/offline.php ("Site is offline" error page)
 
  
To render the error backtrace information when debugging mode is on you can call the renderBacktrace method like this:
+
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 the your ''templates/<template-name>/error.php'' file to reference the new location of the stylesheet by changing this line, accordingly:
 +
<source lang="php"><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.
 +
 +
==== 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.
 
<source lang="php">
 
<source lang="php">
<?php if ($this->debug) :
+
<?php if ($this->error->code = '404') { ?>
echo $this->renderBacktrace();
+
<div id="errorboxheader">Page not found</div>
endif; ?>
+
<div id="errorboxbody"><p>Sorry! That page cannot be found.</p>
 +
</div>
 +
</div>
 +
<?php } ?>
 
</source>
 
</source>
  
Note that jdoc:include elements are not parsed in these error pages.
+
==== HTTP status code ====
<noinclude>[[Category:Advanced]][[Category:Templates]][[Category:Topics]]</noinclude>
+
 
 +
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:
 +
<source lang="php">
 +
<?php if ($this->error->code = '404') { ?>
 +
header("HTTP/1.0 404 Not Found");
 +
<?php } ?>
 +
</source>
 +
 
 +
'''More HTTP status code information:'''
 +
 
 +
* [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]

Revision as of 14:30, 4 March 2008

Custom error pages[edit]

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.

Overriding the System Error Results[edit]

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[edit]

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:

<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[edit]

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->code = '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[edit]

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->code = '404') { ?>
	header("HTTP/1.0 404 Not Found");
<?php } ?>

More HTTP status code information: