2. Add a catch-all sitemap item that creates a dynamic 404 page
Assume, you have the following sitemap example:
/sitemap: /home: /news: /**: /agenda: /**:
URLs such as /home, /news/2010, /news/2010/foo, /agenda/2010 all can be matched. But what about the URL /home/foo, or /dossiers/2010. These URLs cannot be matched. Typically, when an old site from another platform is migrated to a HST2 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 Handling error codes and exceptions by the 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.
Adding a catch-all sitemap item
Change your sitemap from above to:
sitemap example extended:
/sitemap: /home: /news: /**: /agenda: /**: /**:
Thus, 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 HST2 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 agenda 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 a error page with. For example a small :
component/page snippet:
/pages: /standard: /errorpage: /body:
Now, errorpage extends for example standard and contains:
hst:componentclassname: org.hippoecm.hst.demo.components.Set404Component
and body is some error page custom body, for example it can do a search for documents that might be meant by the URL.
The Set404Component is very simple just setting the correct header response:
Set404Component:
public class Set404Component extends BaseHstComponent { @Override public void doBeforeRender(HstRequest request, HstResponse response) throws HstComponentException { response.setStatus(404); } }