301 Permanent Redirect in Java

By , 6 October 2010

301 Permanent Redirect in Java

Wow, I just discovered that all the redirects I had written in my Java webapps use 302 temporary redirects. The reason this is concerning is that SEO best practise recommends using 301 permanent redirects to ensure PageRank and page reputation are attributed to the correct (target) page.

The code I was using looks like this:

response.setStatus(response.SC_MOVED_PERMANENTLY);
response.sendRedirect(newURL);

The problem is of course response.sendRedirect() overrides the permanent redirect status. LOL, hooray for shortcut methods.

301 Permanent Redirect in Java

The correct way to do a permanent redirect in Java is like this:

response.setStatus(response.SC_MOVED_PERMANENTLY);
response.setHeader("Location", newURL);

About Roger Keays

301 Permanent Redirect in Java

I guess I'd call myself a problem solver. Either that, or I'm some sort of organic machine designed to convert oxygen into carbon dioxide. You could go either way on that. I'm into languages and stuff. I wrote a book. It's okay, I guess. What else? I like reading, swimming, eating, and playing music. Satisfied? Sheesh.

Leave a Comment

Please visit https://rogerkeays.com/blog/301-permanent-redirect-in-java to add your comments.