Redirect

Redirect allows to complete the execution of the current page or action by redirecting the client to another location.

All your methods set the header Location, send the corresponding HTTP status code and immediately complete the execution.

If you want to know when Response may be modified, consult the documentation of the Lifecycle.


Structure

ctx.Response.Redirect implement the following API:

text
Redirect
{
    Permanent(string)
    Temporary(string)
    SeeOther(string)
}

Methods

MethodCodeDescription
Permanent(location)308 Permanent RedirectIt permanently redirects by preserving the HTTP method and the body of the request.
Temporary(location)307 Temporary RedirectIt temporarily redirects by preserving the HTTP method and the body of the request.
SeeOther(location)303 See OtherRedirects using a new request GET.

Execution flow

When GOX runs any of the methods of Redirect, the current page or action ends immediately and the client is redirected to the indicated location.

gox
if !validate.IsLogin(ctx) {
	// La ejecución finaliza aquí.
	ctx.Response.Redirect.SeeOther("/login")

	// Este return no es necesario.
	return
}

Choose the right method

Use SeeOther() after processing forms or actions to be completed by showing another page.

gox
ctx.Response.Redirect.SeeOther("/dashboard")

Use Temporary() where the redirection is temporary and the HTTP method must be preserved.

gox
ctx.Response.Redirect.Temporary("/maintenance")

Use Permanent() where a route has changed permanently and client must use the new location in future requests.

gox
ctx.Response.Redirect.Permanent("/docs")

Best practices

  • Use SeeOther() to finalize forms and actions.
  • Use Temporary() when the redirection is temporary.
  • Use Permanent() where the change of location is final.
  • Configure Response before redirecting.

See also

Next step

Continue with the documentation for Headers to learn to consult, add, modify and remove HTTP headers from an response.

Headers