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 a303 See Other redirect that instructs the client to change the method to GET:
Redirect::temporary
Creates a307 Temporary Redirect that preserves the original HTTP method and body:
Redirect::permanent
Creates a308 Permanent Redirect for permanent URL changes:
How it works
Type definition
axum/src/response/redirect.rs:22-25.
IntoResponse implementation
TheRedirect type sets the Location header with the specified status code:
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
examples/oauth/src/main.rs:153-184.
OAuth callback redirect
examples/oauth/src/main.rs:317.
Conditional redirects
HTTPS redirect
examples/tls-rustls/src/main.rs.
Using in custom error responses
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
- Response tuples for combining with headers
- IntoResponse trait for custom response types
- StatusCode for other status codes