Skip to main content

redirect_www

Function redirect_www 

Source
pub async fn redirect_www(request: Request, next: Next) -> Response
Expand description

Middleware sending www. + the host of APP_URL to APP_URL, same path and query, with 301 Moved Permanently.

For an app answering on both example.com and www.example.com (two custom domains of the Worker): search engines and links then see one address per page. Requests to any other host, or without APP_URL, go through unchanged. Add it with .layer(axum::middleware::from_fn(ocre::seo::redirect_www)) in routes(). Reads one variable; no billed resource.

ยงExamples

use axum::{Router, middleware::from_fn, routing::get};
use ocre::Ctx;

fn routes() -> Router<Ctx> {
    Router::new().route("/", get(|| async { "home" })).layer(from_fn(ocre::seo::redirect_www))
}