Custom error page

When a user requests a non-existing page, the servlet container (Tomcat) or application server displays a 404 error page and returns the 404 error code. You can configure what happens in the case of a 404 directly in the web.xml configuration file in the container.

A common solution is to create a custom error page that looks like the rest of your site, tells the visitor in clear terms that the page was not found, and provides some alternative ways to continue such as navigation and a search box.

web.xml
<error-page>
   <error-code>404</error-code>
   <location>/error/404.html</location>
</error-page>
Additional error codes

404 is, of course, not the only error code. You can set up custom error pages for any error code using the same approach.

See Site aware exception handling in Magnolia CMS for tips on how to do that.

Using URI mappings to handle error pages

You can define URI mappings for all error pages.

  • web.xml

  • defaultErrorHandler.yaml

  • statusErrorHandler.yaml

/WEB-INF/web.xml
...
  <error-page>
    <error-code>403</error-code>
    <location>/.error/403</location>
  </error-page>
  <error-page>
    <error-code>404</error-code>
    <location>/.error/404</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/.error/500</location>
  </error-page>
  <error-page>
    <location>/.error</location>
  </error-page>
...
custom-light-module/virtualUriMappings/defaultErrorHandler.yaml
class: info.magnolia.virtualuri.mapping.RegexpVirtualUriMapping
fromUri: /.error.*
toUri: forward:/docroot/error.html
custom-light-module/virtualUriMappings/statusErrorHandler.yaml
class: info.magnolia.virtualuri.mapping.RegexpVirtualUriMapping
fromUri: /.error/(403|404)
toUri: forward:/docroot/$1.html

These allow you to create decoration-based redirects to custom error pages (to a FreeMarker page or to a static resource) without the need to modify the configuration in the servlet container.

Feedback

DX Core

×

Location

This widget lets you know where you are on the docs site.

You are currently perusing through the DX Core docs.

Main doc sections

DX Core Headless PaaS Legacy Cloud Incubator modules