When building a website, there is one inevitable thing: 301 permanent redirection. The cases that have to involve 301 includes:
- Redirection from www subdomain to naked domain or vise versa.
- Redirection from slashed pretty URL to unslashed URL or vise versa.
301 is easy to implement with Go:
func redirect301(w http.ResponseWriter, url string) {
w.Header().Set("Location", url)
w.WriteHeader(http.StatusMovedPermanently)
}
There is one more thing that needs attention: The root path of a domain always contains a slash (GET / in HTTP request), regardless the user enters the slash or not, so the root path needs no redirection.