Saturday, March 01, 2008

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: -->>

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 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:

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 = _originalResponseStream;

String path = getAbsolutePath(url);

CharBuffer cb = new CharBuffer();

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

if (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


No comments:

C# scan cs file and find variables values and names

using Microsoft . CodeAnalysis ; using Microsoft . CodeAnalysis . CSharp ; using Microsoft . CodeAnalysis . CSharp . Syntax ; using Xunit . ...