# Spring MVC Controller怎么处理301跳转 ## 1. 什么是301跳转 301跳转(301 Redirect)是HTTP协议中的一种永久性重定向状态码。当服务器返回301状态码时,表示请求的资源已被永久移动到新的URL,浏览器或爬虫会自动跳转到新地址,并将原地址的权重传递给新地址。 ### 301跳转的特点: - **永久性**:告知搜索引擎此重定向是永久性的 - **权重传递**:SEO权重会从旧URL转移到新URL - **缓存**:浏览器会缓存重定向结果 ## 2. Spring MVC中实现301跳转的方式 在Spring MVC中,有多种方式可以实现301跳转: ### 2.1 使用`RedirectView` ```java @Controller public class RedirectController { @GetMapping("/old-url") public RedirectView redirectWithRedirectView() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("/new-url"); redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); // 设置301状态码 return redirectView; } } ResponseEntity@Controller public class RedirectController { @GetMapping("/old-page") public ResponseEntity<Void> redirectWithResponseEntity() { HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create("/new-page")); return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY); } } redirect:前缀(仅适用于302)注意:Spring的redirect:前缀默认使用302跳转,不能直接用于301。如果需要301,需结合其他方式:
@Controller public class RedirectController { @GetMapping("/old") public ModelAndView redirectWithModelAndView() { ModelAndView mav = new ModelAndView(); mav.setViewName("redirect:/new"); mav.setStatus(HttpStatus.MOVED_PERMANENTLY); return mav; } } @Controller public class WebsiteRedesignController { @GetMapping("/old/{productId}") public RedirectView redirectProductPage(@PathVariable String productId) { RedirectView redirectView = new RedirectView(); redirectView.setUrl("/products/" + productId); redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); return redirectView; } } @Controller public class DomainChangeController { @GetMapping("/**") public ResponseEntity<Void> redirectToNewDomain(HttpServletRequest request) { String newUrl = "https://newdomain.com" + request.getRequestURI(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(newUrl)); return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY); } } @Controller public class HttpsRedirectController { @GetMapping("/secure/**") public ResponseEntity<Void> redirectToHttps(HttpServletRequest request) { String httpsUrl = "https://" + request.getServerName() + request.getRequestURI(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(httpsUrl)); return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY); } } 使用curl命令测试:
curl -v http://localhost:8080/old-url 预期输出应包含:
HTTP/1.1 301 Location: /new-url | 状态码 | 类型 | SEO影响 | 缓存 | Spring MVC实现方式 |
|---|---|---|---|---|
| 301 | 永久重定向 | 权重传递 | 是 | RedirectView/ResponseEntity |
| 302 | 临时重定向 | 不传递权重 | 否 | redirect:前缀 |
| 307 | 临时重定向 | 保持方法和请求体 | 否 | HttpStatus.TEMPORARY_REDIRECT |
RedirectController管理所有重定向规则@Controller public class CentralRedirectController { @Autowired private RedirectMappingService redirectService; @GetMapping("/**") public ResponseEntity<Void> handleRedirect(HttpServletRequest request) { String requestedUri = request.getRequestURI(); String targetUrl = redirectService.getTargetUrl(requestedUri); if (targetUrl != null) { HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(targetUrl)); return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY); } throw new ResourceNotFoundException(); } } Spring MVC提供了灵活的方式来实现301永久重定向,开发者可以根据具体场景选择RedirectView、ResponseEntity等方式。正确的使用301跳转不仅能提升用户体验,还能保持网站的SEO价值。在实际应用中,应注意跳转的缓存特性、性能影响,并遵循最佳实践来管理重定向规则。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。