Add a Catch-All Sitemap Item that creates a Dynamic 404 Page
Assume, you have the following sitemap example:
/sitemap: /home: /news: /**: /events: /**:
URLs such as /home, /news/2019, /news/2019/foo, /events/2019 can all be matched. But what about the URL /home/foo, or /files/2010. These URLs cannot be matched. Typically, when an old site from another platform is migrated to a HST-based implementation, all kind of websites and search engines contain old links that do not work anymore. For example /home.aspx.
When a URL cannot be matched, a org.hippoecm.hst.core.container.ContainerNotFoundException is propagated all the way to the web container, where you can catch it in the web.xml (see Handle Error Codes and Exceptions in web.xml. However, you can then not have a dynamic error page, with a dynamic repository based menu, or for example, a more clever error page showing a list of URL suggestions which you might be looking for.
Add a Catch-All Sitemap Item
Change your sitemap from above to:
sitemap example extended:
/sitemap: /home: /news: /**: /events: /**: /**:
We have added one ** matcher to the root. It does not matter whether it is located at the end or at the beginning as the HST URL matching tries to match the best match instead of the first. Also, now, it is clear that everything that does not match to the home, news, and events items and subitems, will match **.
Now, all you need to do, is from the ** sitemap item, point to some hst:componentconfigurationid that contains all the HST components to create an error page with, like in this example snippet:
/pages: /standard: /errorpage: /body:
Now, errorpage extends for example standard and contains:
hst:componentclassname: org.onehippo.cms7.essentials.components.EssentialsPageNotFoundComponent
and body is some error page custom body, for example it can do a search for documents that might be meant by the URL.
EssentialsPageNotFoundComponent is a standard component class provided by Essentials and takes care of setting the correct response status code:
public class EssentialsPageNotFoundComponent extends CommonComponent { @Override public void doBeforeRender(final HstRequest request, final HstResponse response) { super.doBeforeRender(request, response); response.setStatus(HttpServletResponse.SC_NOT_FOUND); } }