sendRedirect and SEO in java

simply response.sendRedirect takes user to another page. mostly its enough for forwarding browser to somewhere else from user perspective but what about google bot and other search engines. PR is not a easy thing to earn. and not affordable thing to loose.

anyway back to the topic. let say we are redirecting clients for subdomains to the other folders. like this:

faq.domain.com -->> faq.domain.com/faq/

in this situation we should use sendRedirect but that function uses HTTP 302 for the response status it means moved temporarily. as a result if you got any link from pr5 to faq.domain.com you will loose it. because google bot will say that this is a temporarily issue then I should not give this pr to that page.

how can we gain back that pr. you shoul use these lines for redirect:
response.setStatus(301);
response.setHeader("Location",newUrl);

if anyone who wonder about the real implementation of sendRedirect here:

public void sendRedirect(String url)
throws IOException
{
if (url == null)
throw new NullPointerException();

if (_originalResponseStream.isCommitted())
throw new IllegalStateException(L.l("Can't sendRedirect() after data has committed to the client."));

_responseStream.clearBuffer();
_originalResponseStream.clearBuffer();

_responseStream = _originalResponseStream;
resetBuffer();

setStatus(SC_MOVED_TEMPORARILY);
String path = getAbsolutePath(url);

CharBuffer cb = new CharBuffer();

for (int i = 0; i < path.length(); i++) {
char ch = path.charAt(i);

if (ch == '<')
cb.append("%3c");
else
cb.append(ch);
}

path = cb.toString();

setHeader("Location", path);

// The data is required for some WAP devices that can't handle an
// empty response.
ServletOutputStream out = getOutputStream();
out.println("The URL has moved <a href=\"" + path + "\">here</a>");
// closeConnection();

if (_request instanceof AbstractHttpRequest) {
AbstractHttpRequest request = (AbstractHttpRequest) _request;

request.saveSession(); // #503
}

close();
}

Comments

Popular posts from this blog

Postgresql group by day, week and month examples

Spring-boot vs Quarkus