redirecting http to https

97 views
Skip to first unread message

Julia Ma

unread,
Sep 24, 2019, 7:37:02 PM9/24/19
to golang-nuts
Hi all,
I recently realized that request.URL.Scheme is not the correct field to check whether a request is https or not (thank you to this thread and other posts on the internet). Once I've determined that the request is http, what do I set the request.TLS to to ensure the redirect is https?
Thanks,
Julia

Diego Medina

unread,
Sep 25, 2019, 12:32:35 PM9/25/19
to golang-nuts
You can run a go routine similar to:


go func() {
        log.Fatal(http.ListenAndServe(":80", http.HandlerFunc(httpToHTTPS)))
}()


//httpToHTTPS redirects all http to https
func httpToHTTPS(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, "https://"+r.Host+r.URL.String(), http.StatusMovedPermanently)
}


Regards,

Diego

Dimas Prawira

unread,
Sep 25, 2019, 2:26:56 PM9/25/19
to Julia Ma, golang-nuts
I would rather to use nginx for redirecting from http to https.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9a23f15a-e398-4f7d-9a1c-12990f41d136%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages