Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Info

You can reference here for a Site aware exception handling in Magnolia CMS which is updated for Magnolia CMS 5.7.+


The 404 Handler

In some projects you may want to have the possibility to define your own Error-Pages (as 404 etc.).
With magnolia it is either possible to define this pages internally in the website tree, or externally.

...

If you are using Tomcat/Jboss you can do so by simple edit the magnoliaPublic web.xml (inside webapps\magnoliaPublic\WEB-INF\web.xml)
and add the following section:

Code Block
xml
xml
<error-page>
   <error-code>404</error-code>
   <location>/errorPages/404.html</location>
</error-page>

When using a single installation with multiple domains, host based virtual URI mapping might be useful:

Code Block
xml
xml
<!--
    The error handler for HTTP 404 Not Found, and alike.

    The page "/my-virtual-uri-mapping/error-404" does not exist, but
    is used in some URL rewriting configured like below:

    - The rewriting uses "ends-with" mappings, so "magnolia-cms.de" also
      matches "www.magnolia-cms.de". And simply using ".de" matches both
      domains as well.

    - When there are multiple matches, the longest match is selected.

    - If there's no match, then the mapping in "toUri" is used.

    - Using "forward:" ensures that Tomcat preserves the actual HTTP
      error code, making browsers and search engines understand that the
      page is actually an error page, even though a normal Magnolia page
      is shown to the visitor.

    Configuration
      + modules
        :
        + adminInterface
          :
          + virtualURIMapping
            :
            + my-error-forbidden
              + hosts
                - 1      .de=forward:/de/some/magnolia/forbidden/page
                - 2      .com=forward:/en/some/other/forbidden/page
              - class    info.magnolia.cms.beans.config.HostBasedVirtualURIMapping
              - fromURI  /my-virtual-uri-mapping/error-403
              - toURI    forward:/en/some/default/magnolia/forbidden/page
            + my-error-notfound
              + hosts
                - 1      .de=forward:/de/some/magnolia/notfound/page
                - 2      .com=forward:/en/some/other/notfound/page
              - class    info.magnolia.cms.beans.config.HostBasedVirtualURIMapping
              - fromURI  /my-virtual-uri-mapping/error-404
              - toURI    forward:/en/some/default/magnolia/notfound/page
-->
<error-page>
    <error-code>403</error-code>
    <location>/my-virtual-uri-mapping/error-403</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/my-virtual-uri-mapping/error-404</location>
</error-page>

A 500 Internal Server Error might best be served without relying on Magnolia. Hence:

Code Block
xml
xml
<error-page>
   <error-code>500</error-code>
    <!--
        Magnolia's default "/docroot" is known in server, filters, cms, bypasses.
        When using another path, be sure to add a bypass for that path as well.
    -->
   <location>/errorPagesdocroot/404error-500.html</location>
  </error-page>

After your application server restarted, the error page will now come out of magnolia (with all it's benefits).

...

For this example let's assume that your magnolia site is under /index/
and that you have created your own 404.html error page under that..ie

Code Block

/index/404.html

Edit the magnoliaPublic web.xml (inside webapps\magnoliaPublic\WEB-INF\web.xml)
and add the following section:

Code Block

<error-page>
     <error-code>404</error-code>
     <location>/admintemplates/404.jsp</location>
  </error-page>

Now, create a 404.jsp file inside your \webapps\magnoliaPublic\admintemplates directory
that contains the following jsp code

Code Block

<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String contextName = request.getContextPath();
String newLocn = contextName + "/index/404.html";
response.setHeader("Location",newLocn);
%>

...