404 Error Handler Configuration | Community
Skip to main content
Level 2
May 14, 2024
Solved

404 Error Handler Configuration

  • May 14, 2024
  • 3 replies
  • 2726 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Rohan_Garg

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

 

3 replies

Madhur-Madan
Community Advisor
Community Advisor
May 14, 2024

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

TenuAuthor
Level 2
May 14, 2024

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?

Madhur-Madan
Community Advisor
Community Advisor
May 14, 2024

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

Rohan_Garg
Community Advisor
Rohan_GargCommunity AdvisorAccepted solution
Community Advisor
May 14, 2024

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

 

TenuAuthor
Level 2
May 14, 2024

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

kautuk_sahni
Community Manager
Community Manager
May 16, 2024

@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