4

I am trying to make a multilingual website that redirects you to the homepage in the corresponding language when accessing it's / route, but for some reason Axum is no being able to redirect, but only when setting the redirection from the / route and routing normalization is active.

use axum::{
    http::{header::HeaderMap, StatusCode},
    response::{IntoResponse, Redirect},
    routing::get,
    Router, ServiceExt,
};
use std::net::SocketAddr;
use tower::layer::Layer;
use tower_http::normalize_path::NormalizePathLayer;

#[tokio::main]
async fn main() {
    let addr = SocketAddr::from(([127, 0, 0, 1], 9090));
    axum::Server::bind(&addr)
        .serve(
            NormalizePathLayer::trim_trailing_slash()
                .layer(
                    Router::new()
                        .route("/", get(redirect))
                        .fallback(handler_404),
                )
                .into_make_service(),
        )
        .await
        .unwrap();
}

async fn redirect(_headers: HeaderMap) -> Redirect {
    Redirect::temporary("/404")
}

async fn handler_404() -> impl IntoResponse {
    (StatusCode::NOT_FOUND, "nothing to see here")
}
[package]
name = "minimal"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
accept-language = "2.0.0"
axum = "0.6.5"
tokio = { version = "1.25.0", features = ["full"] }
tower = "0.4.13"
tower-http = { version = "0.3.5", features = ["normalize-path"] }
use = "0.0.0"
Axum500
  • 41
  • 2

0 Answers0