HTTP Redirect

Hyobyung Han·2022년 4월 16일
0

Web

목록 보기
2/2

Introduction

본 문서에서는 HTTP에서의 redirection을 설명한다.

HTTP Status code 3XX

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는 다음과 같다.

300 Multiple Choices

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.)

301 Moved Permanently

리소스 요청을 한 URL에 대해 영구적으로 리다이렉션을 수행한다. new URL이 response로 주어진다. 즉 Client side에서 리다이렉션 정보를 캐싱하고, 이후의 동일 URL에 대해서 리다이렉션 된 new URL로 요청할 수 있다.

302 Found

임시로 리소스 요청을 한 URI가 임시로 변경되었음을 의미하는 response이다. 즉, Client는 동일 URI를 사용해야 한다. (not cacheable)

303 See Other

Client가 GET request로 다른 URI에 리소스를 요청하도록 리다이렉션할 때 사용된다.

304 Not Modified

Client에게 response가 변경되지 않았음 알리기 위할 때 사용된다. 즉 cache 값이 여전히 유효하다는 것을 알려준다.

305 Use Proxy (Deprecated)

이전 HTTP 버전에서 사용되었으며, 요청된 응답은 프록시에 의해 접근되어야함을 가리킨다. 현재는 deprecated됨.

306 unused

더이상 사용되지 않음. reserved이며 HTTP 1.1 이전에 사용되었음.

307 Temporary Redirect

Client가 요청한 동일한 메서드로 다른 URI에 리소스 요청을 하도록 리다이렉션 하고싶을 때 사용된다. status code 302와 의미는 동일하지만, 307은 method와 body를 변경하지 않고 리다이렉트 해야됨을 보장한다.

308 Permanent Redirect

Client가 요청한 URI가 다른 URI로 영구적으로 변경되었음을 알려준다. 301과 동일한 의미이지만 차이점이 존재한다.
307302의 차이와 동일하게, 첫 번째 요청 시 사용된 method와 다른 method를 두 번째 요청 시 사용할 수 없다는 제약이 있다.

Browser compatibility

모든 redirection 관련 status code가

  • Dsektop
    • Chrome
    • Edge
    • Firefox
    • Internet Explorer (308 의 경우 Windows 10 이전 버전에서 지원하지 않는다.)
    • Opera
    • Safari
  • Mobile
    • WebView Android
    • Chrome Android
    • Firefox for Android
    • Opera Android
    • Safari on iOS
    • Samsung Internet

에서 전부 호환된다.

Summary

HTTP Status CodeHTTP VersionMeaningTemporary / PermanentCacheableRequest Method Subsequent Request
301Moved PermanentlyHTTP/1.0PermanentYesGET / POST may change
302FoundHTTP/1.0Temporarynot by defaultGET / POST may change
303See OtherHTTP/1.1Temporaryneveralways GET
307Temporary RedirectHTTP/1.1Temporarynot by defaultmay not change
308Permanent RedirectHTTP/1.1Permanentby defaultmay not change

Examples in Spring Boot

@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;
}

References

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

0개의 댓글