2012-03-29 2 views
6

GAEで実行しているGoでページ要求をリダイレクトすると、リダイレクトページを表示せずにユーザーのアドレスが正しく表示されます。例えば、ユーザが入力した場合:Golang、GAE、ユーザーをリダイレクトしますか?

www.hello.com/1 

は私がにユーザーをリダイレクトするために私のGoアプリケーションをしたいと思います:について

fmt.Fprintf(w, "<HEAD><meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=/one\"></HEAD>") 

答えて

22

www.hello.com/one 

に頼らず1回限り:

func oneHandler(w http.ResponseWriter, r *http.Request) { 
    http.Redirect(w, r, "/one", http.StatusMovedPermanently) 
} 

これが数回発生した場合は、代わりにリダイレクトハンドラを作成することができます

func redirectHandler(path string) func(http.ResponseWriter, *http.Request) { 
    return func (w http.ResponseWriter, r *http.Request) { 
    http.Redirect(w, r, path, http.StatusMovedPermanently) 
    } 
} 

をし、このようにそれを使用する:GO1を使用してそれらのために

func init() { 
    http.HandleFunc("/one", oneHandler) 
    http.HandleFunc("/1", redirectHandler("/one")) 
    http.HandleFunc("/two", twoHandler) 
    http.HandleFunc("/2", redirectHandler("/two")) 
    //etc. 
} 
5
func handler(rw http.ResponseWriter, ...) { 
    rw.SetHeader("Status", "302") 
    rw.SetHeader("Location", "/one") 
} 
+5

、 'SetHeader'が廃止されました。 'w.Header()。Set(" Status "、" 302 ")を代わりに使用してください。 – hyperslug

関連する問題