Forums de discussion

How to send 404 by the portal to inform google

Denis Ehrich, modifié il y a 14 années.

How to send 404 by the portal to inform google

New Member Envoyer: 1 Date d'inscription: 08/07/09 Publications récentes
Hello,

My environment:
- Liferay 5.2.3 - friendly urls
- search engine connection (third party)

Issue:
1) Google crawler collects my pages. (SearchResult-pages identified wire friedlyUrl)
Example URL: http://localhost:8080/web/guest/detail/-/specific/documentid%3A%22article_category_developement%22/t-false
If a user who found this match at google opens the link, my local searchengine will run and display the requested result.
Examle-Search: My SearchEngine search for: the document with the id = article_category_developement

2) Within my local searchengine I remove some documents. So the former link to document XYZ does not exist anymore.
If a user find the link to this document at google he will get an empty page. Thats no problem, but liferay sends state 200 (HttpServletResponse.SC_OK)
The problem is: Google does not realize that this page returns no suitable result.

3) I like to implement a solution: If the requested page returns result with count = 0 (no Search hits) , I like to send the error code 404. So google would be able to realize 404 error and remove this page from its page-list.

The Problem is: Liferay does not send 404 error when my Portlet realizes that there is just an empty result list. Liferay always sends 200. I need a solution to send 404 Error-Code to inform google that the store link is not active anymore.

What I allready tried:

private void throwHttpError(PortletRequest request, PortletResponse response, Integer errorCode, Exception ex){

HttpServletRequest httpServletRequest = null;
HttpServletResponse httpServletResponse = null;
try {
httpServletRequest = PortalUtil.getHttpServletRequest(request);
httpServletResponse = PortalUtil.getHttpServletResponse(response);
} catch (Exception e) {
logger.error("Unable to get HttpServletRequest or HttpServletResponse from Portletrequest/-response.",e);
}
try {
PortalUtil.sendError(errorCode, ex, httpServletRequest, httpServletResponse);
}
catch (IOException e) {
logger.error("Excetion while throwing "+errorCode+" error message.",e);
}
catch (ServletException e) {
logger.error("Excetion while throwing "+errorCode+" error message.",e);
}
}

I called this method with errorCode = 404 after my search-portlet realized that the result set is empty.

What happed: Empty detail page is displayed and the returncode of the portal page is 200.

thanks for help

Pièces jointes:

Jen Fong, modifié il y a 13 années.

RE: How to send 404 by the portal to inform google

New Member Publications: 10 Date d'inscription: 20/05/10 Publications récentes
Hi, Denis--

I knowi t's been over a year since you posted this, but were you ever able to find out how to send a 404 from a portlet?

I see that using a sendRedirect to the 404 page is not allowed from the render phase
http://www.liferay.com/community/forums/-/message_boards/message/4077862?_19_redirect=%2fcommunity%2fforums%2f-%2fmessage_boards%2fsearch%3f_19_redirect%3d%26_19_breadcrumbsCategoryId%3d239390%26_19_breadcrumbsMessageId%3d493491%26_19_searchCategoryId%3d239390%26_19_threadId%3d493492%26_19_keywords%3dset%2bhttp%2b404%2bwithin%2bportlet#_19_message_493782


, so is sending the HTTP error code similarly illegal?

We know we can send a redirect with javascript but we would also like to be able to redirect users who do hnot have javascript enabled...
thumbnail
J L, modifié il y a 13 années.

RE: How to send 404 by the portal to inform google

New Member Publications: 6 Date d'inscription: 18/12/10 Publications récentes
Bump! I have a similar problem.

From a regular "action" (I use the default MVCPortlet), I want to output a 404 header (let's say an ID in the querystring targets an item that doesn't exist).

Simplified example:



public void myAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException
{
    HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(actionResponse);
    httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
}



When I load this action from my browser, no 404 header is ouputed at all!


Any help??
thumbnail
jelmer kuperus, modifié il y a 13 années.

RE: How to send 404 by the portal to inform google

Liferay Legend Publications: 1191 Date d'inscription: 10/03/10 Publications récentes
Http status codes are set for the entire page not just for a portlet so it's kind of awkward to set them from a portlet. That said you could try

PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND, new NoSuchYourEntityException, request, response);
thumbnail
Electro Type, modifié il y a 13 années.

RE: How to send 404 by the portal to inform google

New Member Publications: 6 Date d'inscription: 18/12/10 Publications récentes
Thanks for the reply Jelmer.

I tried:

PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND, new Exception("test"), request, response);


But this leads to a "status" page where the error "Internal Server Error" is displayed.

Here are the headers for the request/response:

-----------------------------------

GET /web/guest/home?p_auth=XX&p_p_id=admin_WAR_testportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_pos=1&p_p_col_count=2&_admin_WAR_testportlet_javax.portlet.action=test HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:8080/web/guest
Cookie: GUEST_LANGUAGE_ID=en_US; COOKIE_SUPPORT=true; REMEMBER_ME=true; LOGIN=XX; SCREEN_NAME=XX; JSESSIONID=XX; COMPANY_ID=XX; ID=XX; PASSWORD=XX; PHPSESSID=XX

HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: http://localhost:8080/c/portal/status?status=404&exception=java.lang.Exception&previousURL=%2Fweb%2Fguest%2Fhome%3Fp_auth%3D1TW8zXGZ%26p_p_id%3Dadmin_WAR_testportlet%26p_p_lifecycle%3D1%26p_p_state%3Dnormal%26p_p_mode%3Dview%26p_p_col_id%3Dcolumn-1%26p_p_col_pos%3D1%26p_p_col_count%3D2%26_admin_WAR_testportlet_javax.portlet.action%3Dtest
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Date: Sat, 08 Jan 2011 21:54:34 GMT

-----------------------------------


Liferay seems to be hard to manage for a regular website...
Marcin Antas, modifié il y a 10 années.

RE: How to send 404 by the portal to inform google

New Member Envoyer: 1 Date d'inscription: 07/06/13 Publications récentes
Did anyone find a solution for this issue ?

I'm struggling with the same problem, I have a portlet that is using friendly urls and when a user types an url that doesn't exist I would like to send the 404 status code so that google doesn't index this particular url, but this seems to be impossible with Liferay.