April 27, 2020
Estimated Post Reading Time ~

How to Customize/Configure the 404 error handler for multi-sites in AEM/Adobe CQ5?

This post will explain how to configure the 404 error handler in the multi-site scenario for AEM - This will configure different 404 pages for different sites.

If the error handler is configured for the first time then copy /libs/sling/servlet/errorhandler to /apps/sling/servlet (create the folder structure before copying )

Modify /apps/sling/servlet/errorhandler/404.jsp file to modify the 404 error handling rules.

<%  
//setting response code as 404
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
try {
    String uri = request.getRequestURI();
         
    if(uri.matches("(/content/Sample/en/)(.*)"))
    {
        pageContext.include("/content/Sample/en/404.html");
    } else if(uri.matches("(/content/Sample/es/)(.*)"))
    {

        pageContext.include("/content/Sample/es/404.html");
    } else if(uri.matches("(/en/)(.*)"))
    {
        pageContext.include("/content/Sample/en/404.html");
    }
    else if(uri.matches("(/es/)(.*)"))
    {
        pageContext.include("/content/Sample/es/404.html");
    } else
    {
        pageContext.include("/content/Sample/en/404.html");
    }

} catch (Exception e) {

%>
        Page Not Found
<%
}

%>

The conditions can be added to handle different sites, if the sites are running on different virtual hostnames then get the server name from the request
(request.getServerName()) and add the condition based on the server name.

This will redirect the user to site-specific 404 pages.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.