RedirectResponse creates HTTP redirect responses, sending clients to a different URL. It supports various redirect types through different HTTP status codes.
Import
Class Signature
Constructor Parameters
The URL to redirect to. Can be absolute (
https://example.com) or relative (/path).The HTTP redirect status code. Common values:
301- Moved Permanently (permanent redirect, method may change to GET)302- Found (temporary redirect, method may change to GET)303- See Other (redirect after POST, method changes to GET)307- Temporary Redirect (temporary, preserves method)308- Permanent Redirect (permanent, preserves method)
Additional HTTP headers to include in the response.
Background task to run after sending the redirect.
Usage
Basic Redirect
Redirect to an external URL:Relative Redirect
Redirect to another endpoint in your API:Permanent Redirect (301)
Use for permanently moved resources:Temporary Redirect (307)
Default behavior, preserves the HTTP method:See Other (303)
Redirect after POST to a GET endpoint:With Custom Headers
Add custom headers to the redirect:Conditional Redirect
Redirect based on conditions:HTTP Redirect Status Codes
301 - Moved Permanently
- Use when the resource has permanently moved
- Browsers and search engines update their bookmarks/indexes
- Future requests should use the new URL
- May change request method to GET
302 - Found
- Temporary redirect
- Original URI should be used for future requests
- May change request method to GET
- Most commonly used historically, but 307/308 are preferred now
303 - See Other
- Used after POST/PUT/DELETE operations
- Always changes the request method to GET
- Common pattern: POST to create resource, then 303 to GET that resource
307 - Temporary Redirect (Default)
- Temporary redirect
- Preserves the HTTP method and body
- Use when method preservation is important
- FastAPI’s default redirect status code
308 - Permanent Redirect
- Permanent redirect
- Preserves the HTTP method and body
- More semantically correct than 301 for modern applications
- Use when permanently moving API endpoints that receive POST/PUT requests
Method Preservation
Codes that preserve method (307, 308)
Codes that may change to GET (301, 302, 303)
Notes
- The default status code is
307(Temporary Redirect) - Use absolute URLs for external redirects
- Relative URLs are resolved relative to the current request URL
- Redirect responses should not include a response body
- Be cautious with redirect loops (A redirects to B, B redirects to A)
- Search engines treat 301 redirects as permanent and update their indexes
Best Practices
- Use 301 for permanent moves: When a resource has permanently moved
- Use 307 for temporary redirects: When you want to preserve the HTTP method
- Use 303 after mutations: Redirect GET after POST/PUT/DELETE (PRG pattern)
- Use 308 for permanent API redirects: When moving API endpoints permanently
- Avoid redirect chains: Redirect directly to the final destination
Common Patterns
Post-Redirect-Get (PRG) Pattern
Prevent duplicate form submissions:Authentication Redirect
Redirect unauthenticated users:Related
- Response - Base response class
- JSONResponse - For JSON responses
- HTTPException - For error responses