Randomize
Randomize

Reputation: 9103

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?

Upvotes: 10

Views: 22166

Answers (1)

JB Nizet
JB Nizet

Reputation: 691625

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

Upvotes: 25

Related Questions