rune-axum-redirect-https
Redirect HTTP requests to HTTPS — Tower middleware for Axum.
A tower::Layer that detects incoming HTTP requests and issues a permanent redirect to the equivalent HTTPS URL. HTTPS requests are forwarded to the inner service unchanged. Works with Axum, Hyper, and any Tower-compatible framework.
How detection works
The layer checks for HTTP in this priority order:
X-Forwarded-Proto: http— set by most reverse proxies for plain HTTP upstream connections.- URI scheme of
http— present in direct (non-proxy) connections.
If neither indicator is present the request passes through unchanged.
Installation
[]
= "0.1"
Usage
Secure defaults (308 Permanent Redirect)
use ;
use RedirectHttpsLayer;
let app: Router = new
.route
.layer;
Custom configuration
use ;
use StatusCode;
use ;
// HTTP on :8080 → HTTPS on :8443, using 301 for legacy clients
let layer = new;
let app: Router = new
.route
.layer;
Configuration
| Method | Default | Description |
|---|---|---|
.status(StatusCode) |
308 |
Redirect status code. 308 preserves the HTTP method; use 301 for legacy compatibility. |
.https_port(u16) |
none | HTTPS port in the redirect URL. Useful for non-standard port setups (e.g. 8080 → 8443). |
[!WARNING]
301 Moved Permanentlycauses many browsers and HTTP clients to convert POST requests to GET. Prefer308 Permanent Redirectunless you have a specific compatibility requirement.
[!NOTE] When no
Hostheader is present the request is forwarded unchanged rather than returning a redirect without aLocationheader.
Output
An HTTP request to http://example.com/login produces:
HTTP/1.1 308 Permanent Redirect
location: https://example.com/login
License
MIT