Java redirect 301 전송 방법

I am using the code like:

@RequestMapping(value="/oldPath")
public String doProductOld(
    @PathVariable(value = "oldProductId") Long oldProductId,
   Model model
) throws Exception {
    Product product = productDao.findByOldId(oldProductId);

    return "redirect:" + product.getUrlNewPath();
 }

All works fine but this redirect returns 302 response code instead of 301 which is needed for SEO. How to easily (without ModelAndView or Http response) update it to return 301 code?

PS. I found solutions when ModelAndView object return from controller but need solution for Tiles when tiles alias (String) is returned.

Java redirect 301 전송 방법

fejese

4,5314 gold badges28 silver badges36 bronze badges

asked Dec 24, 2014 at 20:48

2

Try to add @ResponseStatus annotation for your method, see below an example:

@ResponseStatus(HttpStatus.MOVED_PERMANENTLY/*this is 301*/)
@RequestMapping(value="/oldPath")
public String doProductOld(...) throws Exception {
    //...
    return "redirect:path";
}

answered Dec 24, 2014 at 23:04

Java redirect 301 전송 방법

PianovPianov

1,3938 silver badges16 bronze badges

1

General idea is:

@RequestMapping(value="/oldPath")
public ModelAndView doProductOld(
    @PathVariable(value = "oldProductId") Long oldProductId,
   Model model
) throws Exception {
    Product product = productDao.findByOldId(oldProductId);
    RedirectView red = new RedirectView(product.getUrlNewPath(),true);
    red.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
    return new ModelAndView(red);
 }

answered Dec 24, 2014 at 22:32

Java redirect 301 전송 방법

msangelmsangel

9,5783 gold badges51 silver badges68 bronze badges

3

There is a newer, simpler way, add this before the return "redirect:"...:

request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.MOVED_PERMANENTLY);

[sic] you have to set the attribute on the Request object.

answered May 29, 2017 at 0:03

Alex RAlex R

11.3k13 gold badges90 silver badges170 bronze badges

2

Can be done in WebMvcConfigurer also:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("oldpath","newpath").setStatusCode(HttpStatus.MOVED_PERMANENTLY);
}

answered Nov 25, 2015 at 21:07

- 리다이렉션 

HTTP 상태코드가 3XX인 응답을 웹 브라우저에서 받는다. 응답된 메세지 헤더에 Location이 존재한다면 Location 위치로 자동 이동한다. "이동한다"의 뜻은 Location 을 다시 서버에 요청하여 해당 페이지로 이동한다는 의미다.

- 영구 리다이렉션 (Permanent Redirection)

301 Moved Permanently

- 원래 쓰던 uri 가 아닌 새로운 uri로 영구적으로 바뀌었을 때 사용한다.

1) GET /event 요청

2) 서버에서 301 Moved PermanentlyLocation: /new-event 를 클라이언트로 전송

3) 클라리언트에서는 GET /new-event 로 다시 서버에 요청 ( 이때 만약 본문이 존재했다면 제거될 확률 높음)

4) 서버에서 응답하여 페이지를 제공한다. ( 200 OK)

308 Permanent Redirect

- 301 과 같음. 다만 처음 요청시 본문(payload)을 유지한채로 다시 서버에 요청한다.

- 일시적 리다이렉션

302 Found ( PRG = Post -> Redirect -> Get )

1) 웹브라우저에서 서버로 Post /order 로 요청

2) 서버는 웹 브라우저로 302 FoundLocation: /order/20 을 담은 HTTP 메세지 전송

3) 웹 브라우저는 GET /order/20 으로 자동 리다이렉트 

4) 서버는 웹 브라우저에게 /order/20 에 관한 페이지와 200 OK 전송

많은 라이브러리들이 302를 PRG로 거의 표준처럼 사용하고 있어서 사용하는데 큰 문제는 없음.

304 Not Modified 

웹 브라우저에서는 캐시 저장소에 이미지 같은 것들을 저장하고 사용하고 있다. 매번 서버로 이미지를 다운로드 하면 효율이 떨어지기 때문에 캐시 저장소에 이미지를 저장해두고 서버에 요청할때 해당 리소스가 변경이 없다면 서버에서 웹 브라우저로 304 Not Modified를 응답하여 웹 브라우저에서 캐시저장소에 있는 리소스를 사용하도록 한다.

즉, 웹 브라우저의 캐시로 리다이렉트한다.