URL redirection in Java return 302 instead of 301

耗尽温柔 提交于 2019-11-29 01:50:36

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/8756665/url-redirection-in-java-return-302-instead-of-301

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!