tower-canonical-redirect
A tower middleware to enforce canonical hosts in HTTP requests. Useful when you want to redirect website users from insecure http connections and/or www subdomains to a single canonical https domain.
The middleware uses framework-agnostic http and futures abstractions, making it compatible with other crates such as hyper, axum, tonic, warp, etc.
Features
- Enables redirecting HTTP requests from any valid host to another, preserving path and query.
- Works behind reverse proxies by parsing
Forwarded,X-Forwarded-Proto,X-Forwarded-Host, or custom headers. - Defaults to
308 Permanent Redirect, but can be configured to use307 Temporary Redirectfor specific origins.
Example
Basic usage with axum:
use CanonicalRedirectLayer;
async
Using the builder API:
let layer = builder
.proto_header
.host_header
.temporary_origin
.temporary_origin
.build
.unwrap;
License
All code in this repository is free and open source software distributed under the terms of either license, at your option: