Skip to main content
The Redirect response type creates redirect responses using the Location header with various redirect status codes (303, 307, 308).

Basic usage

Create a redirect using one of three constructors:

Methods

Redirect::to

Creates a 303 See Other redirect that instructs the client to change the method to GET:
This is useful after successful form submissions or file uploads when you don’t want the redirected page to observe the original request method and body.

Redirect::temporary

Creates a 307 Temporary Redirect that preserves the original HTTP method and body:

Redirect::permanent

Creates a 308 Permanent Redirect for permanent URL changes:

How it works

Type definition

See implementation in axum/src/response/redirect.rs:22-25.

IntoResponse implementation

The Redirect type sets the Location header with the specified status code:
See implementation in axum/src/response/redirect.rs:87-94.
If the location URI contains invalid characters (like newlines), the response will return 500 Internal Server Error.

Status codes explained

303 See Other (Redirect::to)

  • Behavior: Forces the client to use GET for the subsequent request
  • Use case: After POST form submissions, file uploads, or any operation where you want to prevent resubmission
  • Method change: YES - any method becomes GET

307 Temporary Redirect (Redirect::temporary)

  • Behavior: Preserves the original HTTP method and body
  • Use case: Temporary maintenance pages, A/B testing, or temporary URL changes
  • Method change: NO - POST stays POST, GET stays GET

308 Permanent Redirect (Redirect::permanent)

  • Behavior: Indicates permanent URL relocation while preserving method and body
  • Use case: Permanent URL structure changes, domain migrations
  • Method change: NO - method is preserved

Response fields

header
The URI to redirect to. Set from the parameter passed to the constructor.
StatusCode
The HTTP status code: 303 (See Other), 307 (Temporary Redirect), or 308 (Permanent Redirect)

Common patterns

Post-login redirect

Example from examples/oauth/src/main.rs:153-184.

OAuth callback redirect

Example from examples/oauth/src/main.rs:317.

Conditional redirects

HTTPS redirect

Example pattern from examples/tls-rustls/src/main.rs.

Using in custom error responses

Example from examples/oauth/src/main.rs:320-326.

Helper methods

status_code()

Returns the HTTP status code of the redirect:

location()

Returns the redirect URI as a string slice:

See also