# Advanced Routing
Actix Web features a powerful routing engine. Beyond basic path matching, you can use Guards to filter requests based on headers, query parameters, or complex custom logic.
##Routing Resolution
When a request arrives, Actix Web evaluates routes in the order they were registered. It stops at the first route that matches both the path and all applied guards.
App::new()
.route("/path", web::get().to(handler_a)) // Evaluated first
.route("/path", web::post().to(handler_b)) // Evaluated second##Using Guards
Guards allow you to attach conditions to a route. A route is only executed if the path matches AND all guards return true.
Actix Web provides built-in guards in actix_web::guard:
- Get(), Post(), etc. (Filter by HTTP Method)
- Header("key", "value")
- Host("www.rust-lang.org")
use actix_web::{guard, web, App, HttpResponse, Responder};
async fn secret_handler() -> impl Responder {
"You provided the secret header!"
}
App::new().route(
"/secret",
web::get()
.guard(guard::Header("X-Secret-Token", "supersecret"))
.to(secret_handler)
)##Custom Guards
You can implement your own guards by implementing the guard::Guard trait, or simply passing a closure that takes a &guard::GuardContext and returns a bool.
use actix_web::guard::GuardContext;
fn custom_guard(ctx: &GuardContext<'_>) -> bool {
// Check something complex
ctx.head().uri.query().unwrap_or("").contains("allow=true")
}
App::new().route(
"/custom",
web::get().guard(actix_web::guard::fn_guard(custom_guard)).to(|| async { "Allowed!" })
)##Normalizing Paths
Sometimes clients request /resource/ with a trailing slash, and sometimes /resource without. By default, Actix Web treats these as strictly different. You can use middleware to normalize paths:
use actix_web::middleware::NormalizePath;
App::new()
.wrap(NormalizePath::trim())
// Now both /test and /test/ will route to /test