본 문서에서는 HTTP에서의 redirection을 설명한다.
HTTP에서 3XX status code로 response를 해서 redirect한다. (status code는 항상 3으로 시작한다.) browser는 해당 status code를 받으면 다른 URL을 요청한다.
만약 client가 redirect를 받으면 status code를 보고 redirect를 어떻게 처리할지 결정해야 한다. 예를 들어 client는 status code를 보고 어떻게 caching할지, 이후의 request 방식은 어떻게 할지 ...)
redirection status code는 다음과 같다.
The request has more than one possible response. The user agent or user should choose one of them. (There is no standardized way of choosing one of the responses, but HTML links to the possibilities are recommended so the user can pick.)
리소스 요청을 한 URL에 대해 영구적으로 리다이렉션을 수행한다. new URL이 response로 주어진다. 즉 Client side에서 리다이렉션 정보를 캐싱하고, 이후의 동일 URL에 대해서 리다이렉션 된 new URL로 요청할 수 있다.
임시로 리소스 요청을 한 URI가 임시로 변경되었음을 의미하는 response이다. 즉, Client는 동일 URI를 사용해야 한다. (not cacheable)
Client가 GET request로 다른 URI에 리소스를 요청하도록 리다이렉션할 때 사용된다.
Client에게 response가 변경되지 않았음 알리기 위할 때 사용된다. 즉 cache 값이 여전히 유효하다는 것을 알려준다.
이전 HTTP 버전에서 사용되었으며, 요청된 응답은 프록시에 의해 접근되어야함을 가리킨다. 현재는 deprecated됨.
더이상 사용되지 않음. reserved이며 HTTP 1.1 이전에 사용되었음.
Client가 요청한 동일한 메서드로 다른 URI에 리소스 요청을 하도록 리다이렉션 하고싶을 때 사용된다. status code 302
와 의미는 동일하지만, 307
은 method와 body를 변경하지 않고 리다이렉트 해야됨을 보장한다.
Client가 요청한 URI가 다른 URI로 영구적으로 변경되었음을 알려준다. 301
과 동일한 의미이지만 차이점이 존재한다.
307
과 302
의 차이와 동일하게, 첫 번째 요청 시 사용된 method와 다른 method를 두 번째 요청 시 사용할 수 없다는 제약이 있다.
모든 redirection 관련 status code가
308
의 경우 Windows 10 이전 버전에서 지원하지 않는다.)에서 전부 호환된다.
HTTP Status Code | HTTP Version | Meaning | Temporary / Permanent | Cacheable | Request Method Subsequent Request |
---|---|---|---|---|---|
301 | Moved Permanently | HTTP/1.0 | Permanent | Yes | GET / POST may change |
302 | Found | HTTP/1.0 | Temporary | not by default | GET / POST may change |
303 | See Other | HTTP/1.1 | Temporary | never | always GET |
307 | Temporary Redirect | HTTP/1.1 | Temporary | not by default | may not change |
308 | Permanent Redirect | HTTP/1.1 | Permanent | by default | may not change |
@RequestMapping(...)
public RedirectView exampleRedirect() {
RedirectView redirectView;
redirectView.setUrl("http://..."); // set the url to be redirected.
redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); // set our own status code (in this case, 301.)
return redirectView;
}
https://developer.mozilla.org/ko/docs/Web/HTTP/Redirections
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages
https://err0rcode7.github.io/backend/2021/05/07/HTTP_SPEC.html