FAQ
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.

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

Search Discussions

  • Patrick Mylund Nielsen at Mar 12, 2013 at 5:32 pm
    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

    On Tue, Mar 12, 2013 at 12:19 PM, Ricardo Fernandez wrote:

    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.

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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ricardo Fernandez at Mar 12, 2013 at 5:45 pm
    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))*
    *}*


    El martes, 12 de marzo de 2013 18:31:58 UTC+1, Patrick Mylund Nielsen
    escribió:
    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


    On Tue, Mar 12, 2013 at 12:19 PM, Ricardo Fernandez <
    fernandez...@gmail.com <javascript:>> wrote:
    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.

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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Patrick Mylund Nielsen at Mar 12, 2013 at 5:54 pm
    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.

    On Tue, Mar 12, 2013 at 12:45 PM, Ricardo Fernandez wrote:

    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))*
    *}*


    El martes, 12 de marzo de 2013 18:31:58 UTC+1, Patrick Mylund Nielsen
    escribió:
    If you want to keep the path/request params, you can do something like
    this: http://play.golang.org/p/**Yslp7rllOv<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<https://yourhostname.com/foo?bar=baz>
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=utf-8

    <a href="https://yourhostname.**com/foo?bar=baz<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<http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security>


    On Tue, Mar 12, 2013 at 12:19 PM, Ricardo Fernandez <
    fernandez...@gmail.com> wrote:
    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 <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.

    --
    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<https://groups.google.com/groups/opt_out>
    .

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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ricardo Fernandez at Mar 12, 2013 at 10:09 pm
    Perfect! Thank you very much Patrick! You help me a lot!

    El martes, 12 de marzo de 2013 18:54:35 UTC+1, Patrick Mylund Nielsen
    escribió:
    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.


    On Tue, Mar 12, 2013 at 12:45 PM, Ricardo Fernandez <
    fernandez...@gmail.com <javascript:>> wrote:
    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))*
    *}*


    El martes, 12 de marzo de 2013 18:31:58 UTC+1, Patrick Mylund Nielsen
    escribió:
    If you want to keep the path/request params, you can do something like
    this: http://play.golang.org/p/**Yslp7rllOv<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<https://yourhostname.com/foo?bar=baz>
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=utf-8

    <a href="https://yourhostname.**com/foo?bar=baz<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<http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security>


    On Tue, Mar 12, 2013 at 12:19 PM, Ricardo Fernandez <
    fernandez...@gmail.com> wrote:
    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 <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.

    --
    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<https://groups.google.com/groups/opt_out>
    .

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

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 12, '13 at 5:19p
activeMar 12, '13 at 10:09p
posts5
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase