Skip to content

Instantly share code, notes, and snippets.

@ARolek
Last active August 29, 2015 14:24
Show Gist options
  • Save ARolek/bf5147a3767f9397bcec to your computer and use it in GitHub Desktop.
Save ARolek/bf5147a3767f9397bcec to your computer and use it in GitHub Desktop.
HTTP -> HTTPS redirect from Amazon's ELB in Go
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