1 回答

TA贡献1863条经验 获得超2个赞
使用重定向到裸域或委托给原始处理程序的处理程序包装 TLS 服务器处理程序。
问题中的代码使用http.DefaultServeMux作为处理程序的 TLS 服务器。这是一个处理函数,可以重定向或调用 http.DefaultServeMux:
func redirectWWW(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.Host, "www.") {
u := *r.URL
u.Scheme = "https"
u.Host = strings.TrimPrefix(r.Host, "www.")
http.Redirect(w, r, u.String(), http.StatusFound)
return
}
http.DefaultServeMux.ServeHTTP(w, r)
}
在 TLS 服务器中使用此处理程序:
handle.Lv.Println(http.ListenAndServeTLS(database.Config.PORT, database.Config.TLScertfile,
database.Config.TLSkeyfile, http.HandlerFunc(redirectWWW)))
- 1 回答
- 0 关注
- 166 浏览
添加回答
举报