package main import ( "context" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "strings" ) const ( OldDomain = "liteyuki.icu" NewDomain = "liteyuki.org" ) func main() { h := server.Default() h.Use(func(ctx context.Context, c *app.RequestContext) { host := string(c.Host()) if strings.HasSuffix(host, OldDomain) { newHost := strings.TrimSuffix(host, OldDomain) + NewDomain scheme := "https" if proto := c.Request.Header.Get("X-Forwarded-Proto"); len(proto) > 0 { scheme = string(proto) } else if forwarded := c.Request.Header.Get("Forwarded"); len(forwarded) > 0 { // Forwarded: proto=https;host=xxx for _, part := range strings.Split(string(forwarded), ";") { if strings.HasPrefix(part, "proto=") { scheme = strings.TrimPrefix(part, "proto=") break } } } newURL := scheme + "://" + newHost + string(c.Request.RequestURI()) c.Redirect(301, []byte(newURL)) c.Abort() return } }) h.GET("*", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, map[string]interface{}{"message": "pong"}) }) h.Spin() }