Last active
August 29, 2015 14:24
-
-
Save ARolek/bf5147a3767f9397bcec to your computer and use it in GitHub Desktop.
HTTP -> HTTPS redirect from Amazon's ELB in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
"net/http" | |
) | |
func ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
// HTTP -> HTTPS redirects for Amazon ELB | |
// sniff for X-Forwarded-Proto header | |
proto := r.Header.Get("X-Forwarded-Proto") | |
// if our protocol is http, redirect to https | |
if proto == "http" { | |
// build the url, keeping the request params | |
url := fmt.Sprintf("https://%v%v", r.Host, r.RequestURI) | |
// redirect with a 301 permanent | |
http.Redirect(w, r, url, http.StatusMovedPermanently) | |
return | |
} | |
// usually I call mux.ServeHTTP(w, r) here | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment