A
A
Artem Prokhorov2021-12-13 21:29:11
go
Artem Prokhorov, 2021-12-13 21:29:11

How to redirect all requests to a domain to another domain without a specific uri?

http.GET("/", func(c *gin.Context) {
    c.Redirect(302, "https://wifer-test.ru")
  })

Not / login, not / main, etc., but everything that will be written after / (including itself) should be transferred to what I need. That is, /some, /assds, /sasdwdwede to the domain I need. Do not prescribe every single case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-12-13
@kotcich

To do this, it is better to make middleware and do everything you need already there
. either transfer control further, or redirect and complete the processing of the request.
https://github.com/gin-gonic/gin#custom-middleware
Something like this

func DomainRedirect() gin.HandlerFunc {
    return func(c *gin.Context) {
        if ваше_условие {
            c.Redirect(302, "https://wifer-test.ru/" + c.Request.URL.String())
            return
        }
        c.Next()
    }
}
....
r := gin.New()
r.Use(DomainRedirect())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question