Handling redirects
Redirects are the usual way of telling the client that the content was moved, or there is a needs to look somewhere else to accomplish the request. This recipe describes the way to implement redirects with the standard library.
How to do it...
- Open the console and create the folder
chapter09/recipe09
. - Navigate to the directory.
- Create the file
redirect.go
with the following content:
package main import ( "fmt" "log" "net/http" ) func main() { log.Println("Server is starting...") http.Handle("/secured/handle", http.RedirectHandler("/login", http.StatusTemporaryRedirect)) http.HandleFunc("/secured/hadlefunc", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) }) http.HandleFunc("/login", func(w http.ResponseWriter, r...