Redirect http request to https

5,861 views
Skip to first unread message

Ricardo Fernandez

unread,
Mar 12, 2013, 1:19:31 PM3/12/13
to golan...@googlegroups.com
Hi everybody,

There is any way to redirect the http request to https? I want redirect all http://127.0.0.1 to https://127.0.0.1

I've been trying with RedirectHandler(url string, code int) how you can see:

func main() {
authenticator := auth.NewBasicAuthenticator("Train Controler", Secret)
http.HandleFunc("/",  authenticator.Wrap(rootHandler))
http.Handle("/socket", websocket.Handler(websocketServer))
err := http.ListenAndServeTLS("127.0.0.1", "./tls/cert.pem", "./tls/key.pem", nil)
err2 := http.ListenAndServe("127.0.0.1", http.RedirectHandler("https://127.0.0.1", http.StatusFound))
if err != nil || err2 != nil {
log.Fatal(err)
}
}

Thanks a lot for your help, and sorry if it is too easy I am a golang newbie. 

Patrick Mylund Nielsen

unread,
Mar 12, 2013, 1:31:58 PM3/12/13
to Ricardo Fernandez, golang-nuts
If you want to keep the path/request params, you can do something like this: http://play.golang.org/p/Yslp7rllOv

# curl -i localhost:8080/foo?bar=baz
HTTP/1.1 301 Moved Permanently
Date: Tue, 12 Mar 2013 17:29:32 GMT
Location: https://yourhostname.com/foo?bar=baz
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

<a href="https://yourhostname.com/foo?bar=baz">Moved Permanently</a>.


You might want to look into setting a HSTS header as well: http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security


--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ricardo Fernandez

unread,
Mar 12, 2013, 1:45:45 PM3/12/13
to golan...@googlegroups.com, Ricardo Fernandez
Ok, it works only if I am not using http.ListenAndServeTLS(...), which I need use... My point is that i want force everybody to use https.

func redir(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "https://147.83.74.89:4000"+req.RequestURI, http.StatusMovedPermanently)
}
func main() {
authenticator := auth.NewBasicAuthenticator("Train Controler", Secret)
http.HandleFunc("/",  authenticator.Wrap(rootHandler))
http.Handle("/socket", websocket.Handler(websocketServer))
// _ := http.ListenAndServeTLS(listenAddr, "./tls/cert.pem", "./tls/key.pem", nil)
_ := http.ListenAndServe(listenAddr, http.HandlerFunc(redir))

Patrick Mylund Nielsen

unread,
Mar 12, 2013, 1:54:35 PM3/12/13
to Ricardo Fernandez, golang-nuts
Change

// _ := http.ListenAndServeTLS(listenAddr, "./tls/cert.pem", "./tls/key.pem", nil)
_ := http.ListenAndServe(listenAddr, http.HandlerFunc(redir))

to e.g.:

go http.ListenAndServeTLS(listenAddr+":443", "tls/cert.pem", "tls/key.pem", nil)
http.ListenAndServe(listenAddr+":80", http.HandlerFunc(redir))

or

go func() {
        if err := http.ListenAndServeTLS(listenAddr+":443", "tls/cert.pem", "tls/key.pem", nil); err != nil {
                log.Fatalf("ListenAndServeTLS error: %v", err)
        }
}()
if err := http.ListenAndServe(listenAddr+":80", http.HandlerFunc(redir)); err != nil {
        log.Fatalf("ListenAndServe error: %v", err)
}

and you're good to go.

You need to be root to listen on 443 and 80 on most systems. The easiest/safest solution is to listen on e.g. 1443 and 8080 and redirect via e.g. nginx or iptables.

Ricardo Fernandez

unread,
Mar 12, 2013, 6:09:13 PM3/12/13
to golan...@googlegroups.com, Ricardo Fernandez
Perfect! Thank you very much Patrick! You help me a lot!
Reply all
Reply to author
Forward
0 new messages