I am trying to replace the catch-all 404 in Actix-Web with a redirect (307) to a constant URL.
Currently I've implemented it via a handler registered to a default service:
async fn redirector() -> HttpResponse {
HttpResponse::build(StatusCode::TEMPORARY_REDIRECT).append_header((header::LOCATION, "https://tracker.mywaifu.best")).finish()
}
(Commit: Redirect to mywaifu homepage · ckcr4lyf/kiryuu@00d91b5 · GitHub )
But I'm guessing on every missed route now it is constructing this new response, allocating the header memory etc.
Since the redirect I want to return is static, is it possible to somehow do this more efficiently?
jofas
February 4, 2025, 11:27am
2
Try using web::redirect
instead of implementing your own handler. I don't know how much more efficient it will be, but I'm certain there won't be a more efficient way to handle redirects in actix-web that would be worth the effort, unless you really have to deal with extraordinary circumstances in your production workload.
Thanks for the suggestion, I basically ended up doing:
static HOMEPAGE: &'static str = "https://tracker.mywaifu.best";
...
.default_service(web::to(|| async {
Redirect::to(HOMEPAGE)
}))
i.e. use a closure for the default service.
system
Closed
May 6, 2025, 9:49am
4
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.