Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL redirection in Java return 302 instead of 301

I'm using this code to redirect url:

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

what I can see is a correct redirection but the value returned in the response is 302 instead of 301. How can I force it to 301?

like image 840
Randomize Avatar asked Jan 06 '12 10:01

Randomize


1 Answers

If you use sendRedirect, it will reset the status to 302. You'll have to use setHeader to set the Location header yourself to redirect using a 301 status.

Example code:

response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", "http://somewhere/");

Pulled from this answer: HttpServletResponse sendRedirect permanent

like image 75
JB Nizet Avatar answered Sep 20 '22 20:09

JB Nizet