We are working on a website using SDL Tridion 2013 SP1, Oracle 12c (CM & CD) and Java on the CD side. Any invalid URLs on the website (links that don't exist on the website) are generating 500 server error status code rather than sending a 404 file not found. Can someone kindly advice on how to fix this.
-
Check out tridion.stackexchange.com/questions/12686/… - maybe this will help :-)– RjECommented Dec 31, 2015 at 9:28
-
i faced similar issue in dxa.net in very earlier version, DXA try to guess the page if url ends with "/" via suffixing index etc. if that guess url does not exist in broker. it throws 500.– Raj KumarCommented Jan 1, 2016 at 17:04
-
Thank you for your comments. I contacted SDL and was informed that this is a bug in the pre-release version of Java-DXA.– Husain ZahidCommented Jan 5, 2016 at 10:11
Add a comment
|
1 Answer
/**
* Handles a {@code NotFoundException}.
*
* @param request The request.
* @return The name of the view that renders the "not found" page.
*/
public String handleNotFoundException(HttpServletRequest request, HttpServletResponse response) throws Exception {
String path = webRequestContext.getLocalization().getPath();
String notFoundPageUrl = (path.endsWith("/") ? path : path + "/") + "error-404";
Page pageModel;
try {
pageModel = contentProvider.getPageModel(notFoundPageUrl, webRequestContext.getLocalization());
} catch (ContentProviderException e) {
LOG.error("Could not find error page", e);
throw new HTTPException(SC_NOT_FOUND);
}
if (!isIncludeRequest(request)) {
request.setAttribute(PAGE_ID, pageModel.getId());
}
request.setAttribute(PAGE_MODEL, pageModel);
request.setAttribute(LOCALIZATION, webRequestContext.getLocalization());
request.setAttribute(MARKUP, markup);
request.setAttribute(SCREEN_WIDTH, mediaHelper.getScreenWidth());
response.setStatus(SC_NOT_FOUND);
return this.viewResolver.resolveView(pageModel.getMvcData(), "Page", request);
}
The above code fixes the mentioned issues and correctly returns a 404 status code for file not found.