3

I'm trying to support a CQ5 (5.5) installation developed by an outside firm for my company.

It appears that my company wanted a pretty 404 page that looked like the rest of the site, and using the custom Sling 404.jsp error handler to redirect to a regular page that merely says "Page Not Found" was the easiest way to do it. The problem is that the 404 page actually returns a 200 status code since it really is just a regular content page that bears a "Not Found" message on it.

This is causing us problems with Google and the GoogleBot, since Google believes all the old search links to now non-existent pages are still valid (200 status code).

Is there any way to configure CQ to return the appropriate 404 status code for the "not found" HTML page that we display? When I am in the CQ Author mode editing the page, I find nothing in page properties or in components that could be added to the page.

Any help would be appreciated, as CQ is not exactly my area of expertise.

Michael Oryl
  • 20,856
  • 14
  • 77
  • 117

2 Answers2

4

You'll have to overlay /libs/sling/servlet/errorhandler/404.jsp file in order to do so - copy it to /apps/sling/servlet/errorhandler/404.jsp and change according to your specification.

And if you are looking specifically into setting appropriate response status code - you can do it by setting respective response property:

response.setStatus(404);

UPDATE: instead of redirecting to the page_not_found.html you might want to include it to the 404.jsp after setting response status:

<sling:include path="path/page_not_found.html" />
Jura Khrapunov
  • 1,024
  • 6
  • 14
  • We already have a 404.jsp in that location, as I mention in the post, and what it does is redirect to the page_not_found.html URL. In lieu of rebuilding the entire page in JSP, something we lack the skillset for, I am asking if it is possible to change the status code returned on a regular HTML page. – Michael Oryl Oct 19 '15 at 14:53
  • Gotcha, you might want to include target page instead of redirecting to it. I updated answer above with suggested code – Jura Khrapunov Oct 19 '15 at 16:20
  • OK, that sounds feasible. I'll give it a try and let you know. Thanks. – Michael Oryl Oct 19 '15 at 18:26
  • This turned out to be the simplest solution. Thanks for the idea. Much appreciated. – Michael Oryl Oct 20 '15 at 00:24
1

You can set the response code fairly easily with this sort of code: response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);

So for example, a quick-and-dirty implementation on your page_not_found.jsp would be as follows:

<%
response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
%>

(or a longer-term/better implementation would be to set it via a tag and a tag library to avoid scriptlets)

If your page_not_found.html page is a static HTML page and not rendered via a jsp, you may need to change your 404.jsp so it redirects to a page that is rendered via a jsp for this approach to work. The status code is set by the server rendering the response. It is not something intrinsic in the HTML itself, so you won't be able to set this in a regular, static HTML page. Something must be done on the server to set this status code. Also see How to Return Specific HTTP Status Code in a Plain HTML Page

Community
  • 1
  • 1
Shawn
  • 8,374
  • 5
  • 37
  • 60