package main import ( "net/http" ) func redirect(w http.ResponseWriter, req *http.Request) { http.Redirect(w, req, "https://" + req.Host + req.URL.String(), http.StatusMovedPermanently) } func index(w http.ResponseWriter, req *http.Request) { // all calls to unknown url paths should return 404 if req.URL.Path != "/" { http.NotFound(w, req) return } http.ServeFile(w, req, "index.html") } func main() { // redirect every http request to https go http.ListenAndServe(":80", http.HandlerFunc(redirect)) // serve index (and anything else) as https mux := http.NewServeMux() mux.HandleFunc("/", index) http.ListenAndServeTLS(":443", "cert.pem", "key.pem", mux) }