콘텐츠로 이동

리다이렉트

Gin은 HTTP 리다이렉트(클라이언트를 다른 URL로 보내기)와 라우터 리다이렉트(클라이언트 왕복 없이 내부적으로 요청을 다른 핸들러로 전달)를 모두 지원합니다.

적절한 HTTP 상태 코드와 함께 c.Redirect를 사용하여 클라이언트를 리다이렉트합니다:

  • 301 (http.StatusMovedPermanently) — 리소스가 영구적으로 이동했습니다. 브라우저와 검색 엔진이 캐시를 업데이트합니다.
  • 302 (http.StatusFound) — 임시 리다이렉트. 브라우저가 따라가지만 새 URL을 캐시하지 않습니다.
  • 307 (http.StatusTemporaryRedirect) — 302와 유사하지만 브라우저가 원래 HTTP 메서드를 유지해야 합니다(POST 리다이렉트에 유용).
  • 308 (http.StatusPermanentRedirect) — 301과 유사하지만 브라우저가 원래 HTTP 메서드를 유지해야 합니다.
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// External redirect (GET)
router.GET("/old", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "https://www.google.com/")
})
// Redirect from POST -- use 302 or 307 to preserve behavior
router.POST("/submit", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/result")
})
// Internal router redirect (no HTTP round-trip)
router.GET("/test", func(c *gin.Context) {
c.Request.URL.Path = "/final"
router.HandleContext(c)
})
router.GET("/final", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"hello": "world"})
})
router.GET("/result", func(c *gin.Context) {
c.String(http.StatusOK, "Redirected here!")
})
router.Run(":8080")
}
Terminal window
# GET redirect -- follows to Google (use -L to follow, -I to see headers only)
curl -I http://localhost:8080/old
# Output includes: HTTP/1.1 301 Moved Permanently
# Output includes: Location: https://www.google.com/
# POST redirect -- returns 302 with new location
curl -X POST -I http://localhost:8080/submit
# Output includes: HTTP/1.1 302 Found
# Output includes: Location: /result
# Internal redirect -- handled server-side, client sees final response
curl http://localhost:8080/test
# Output: {"hello":"world"}