Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

404 Error Handler Configuration

Avatar

Level 2

Hello, I'm working on a project that has several folders, and from the beginning, we have configured various zones like content/en/en/errors/404 in, for example, Project A's folder, and it redirects errors there. Similarly, we have several folders with their respective customized errors. However, I have created a new folder and the internal errors directory, but when I trigger a 404, it loads a blank 404 page, and I can't figure out how to configure AEM to automatically redirect to that errors area. How should this configuration be set up?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Tenu,

Can you confirm if for the other tenants (folders) have ErrorDocument directive configured in dispatcher module?

Assuming a multilingual content structure, place the following directives in your VHost.

Each vhost file can have a ErrorDocument pointing the location where 404 or 500 based errors should redirect to. 

Alternatively, in some projects you can have a generic errorhandler.conf which is included in every lingual vhost file thereby eliminating the redundancy.

You can also refer to the below links - 

 

Hope this helps!

 

Best Regards,

Rohan Garg

 

View solution in original post

6 Replies

Avatar

Level 7

Hi @Tenu ,

To dynamically handle 404 errors, you can overlay the default error handler script.
Open CRX/DE and navigate to /libs/sling/servlet/errorhandler.
Right-click the errorhandler folder and select “Overlay Node.” Make sure to select the “Match Node Types” checkbox.
Copy the default script 404.jsp from /libs/sling/servlet/errorhandler/ to /apps/sling/servlet/errorhandler/.

OR

You'll need to create a custom servlet to handle HTTP errors and redirect to the appropriate error pages.
This servlet should intercept requests that result in errors (e.g., 404 status code) and redirect them to the corresponding custom error page.
Below is an example of how you can implement a simple error handler servlet

 

package com.example.servlets;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import javax.servlet.Servlet;
import java.io.IOException;

@Component(service = Servlet.class,
           property = {
               "sling.servlet.resourceTypes=sling/servlet/errorhandler",
               "sling.servlet.methods=GET"
           })
public class CustomErrorHandlerServlet extends SlingAllMethodsServlet {
    
    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
        int statusCode = response.getStatus();
        switch (statusCode) {
            case 404:
                response.sendRedirect("/content/myproject/errors/404.html");
                break;
            // Add cases for other error status codes as needed
            default:
                // Default redirect for other error codes
                response.sendRedirect("/content/myproject/errors/default.html");
                break;
        }
    }
}

 


Thanks,
Madhur

Avatar

Level 2

Hello, I'm trying to implement the first option, but I don't understand what you mean by 'the 2'. Should I name the new JSP that, or what are you suggesting?

Avatar

Level 7

That was a typo. I have edited the response. I mean you need to overlay it in you /apps hierarchy

Avatar

Correct answer by
Community Advisor

Hi @Tenu,

Can you confirm if for the other tenants (folders) have ErrorDocument directive configured in dispatcher module?

Assuming a multilingual content structure, place the following directives in your VHost.

Each vhost file can have a ErrorDocument pointing the location where 404 or 500 based errors should redirect to. 

Alternatively, in some projects you can have a generic errorhandler.conf which is included in every lingual vhost file thereby eliminating the redundancy.

You can also refer to the below links - 

 

Hope this helps!

 

Best Regards,

Rohan Garg

 

Avatar

Level 2

No, they are not configured in the dispatcher, or at least after checking with grep, there are no instances of ErrorDocument and they are set to 0 and managed by AEM.

 

edit: i tried adding errorPage variable with path of errors in my projects & works, thx

Avatar

Administrator

@Tenu Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni