RedirectHandler returns a simple redirect handler. See `NewProxy` or `ProxyHandler` for more features.
(target *url.URL, redirectStatus int)
| 183 | // RedirectHandler returns a simple redirect handler. |
| 184 | // See `NewProxy` or `ProxyHandler` for more features. |
| 185 | func RedirectHandler(target *url.URL, redirectStatus int) http.Handler { |
| 186 | targetURI := target.String() |
| 187 | if redirectStatus <= 300 { |
| 188 | // here we should use StatusPermanentRedirect but |
| 189 | // that may result on unexpected behavior |
| 190 | // for end-developers who might change their minds |
| 191 | // after a while, so keep status temporary. |
| 192 | // Note thatwe could also use StatusFound |
| 193 | // as we do on the `Context#Redirect`. |
| 194 | // It will also help us to prevent any post data issues. |
| 195 | redirectStatus = http.StatusTemporaryRedirect |
| 196 | } |
| 197 | |
| 198 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 199 | redirectTo := path.Join(targetURI, r.URL.Path) |
| 200 | if len(r.URL.RawQuery) > 0 { |
| 201 | redirectTo += "?" + r.URL.RawQuery |
| 202 | } |
| 203 | http.Redirect(w, r, redirectTo, redirectStatus) |
| 204 | }) |
| 205 | } |
no test coverage detected
searching dependent graphs…