Java中的URL重定向返回302而不是301 [英] URL redirection in Java return 302 instead of 301

查看:692
本文介绍了Java中的URL重定向返回302而不是301的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码重定向网址:

I'm using this code to redirect url:

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

我能看到的是正确的重定向,但响应中返回的值是302而不是301。如何强制它到301?

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?

推荐答案

如果您使用 sendRedirect ,它会将状态重置为302.您必须使用setHeader自行设置 Location 标题,以使用301状态重定向。

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.

示例代码:

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

从这个答案中拉出来:
HttpServletResponse sendRedirect permanent

Pulled from this answer: HttpServletResponse sendRedirect permanent

这篇关于Java中的URL重定向返回302而不是301的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆