2 回答

TA貢獻1796條經驗 獲得超4個贊
來自go doc http.request:
type Request struct {
...
// For incoming requests, the Host header is promoted to the
// Request.Host field and removed from the Header map.
...
Header Header
...
// For server requests Host specifies the host on which the
// URL is sought. Per RFC 2616, this is either the value of
// the "Host" header or the host name given in the URL itself.
...
Host string
因此,r.Host不使用r.Header.Get("Host")

TA貢獻1775條經驗 獲得超11個贊
嘗試使用 r.Host?
文檔說:
// For server requests Host specifies the host on which the URL
// is sought. Per RFC 7230, section 5.4, this is either the value
// of the "Host" header or the host name given in the URL itself.
那么也許試試看?
func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
http.Redirect(w, r, "https://"+r.Host+r.URL.Path, 301)
return
}
//....
}
- 2 回答
- 0 關注
- 160 瀏覽
添加回答
舉報