我在用warp https://crates.io/crates/warp提供静态文件目录。不幸的是,当我在路径中添加尾部斜杠时,只能解析这些静态文件中使用的相对链接。
这是我用来服务目录的代码:
let route = warp::path("segment")
.and(warp::path("another"))
.and(warp::fs::dir("../path/to/static/files/folder"));
warp::serve(route).run(([0, 0, 0, 0], 3030)).await;
So now
-
0.0.0.0:3030/segment/another/
工作正常
-
0.0.0.0:3030/segment/another
不起作用
原则上我不会介意这一点,并且总是使用带有尾部斜杠的 URL,但是当我从 iOS 上的 Safari(作为 PWA)“添加到主屏幕”页面时,尾部斜杠会自动省略。
因此,为了解决这个问题,我尝试创建一个重定向,然后添加尾部斜杠,如下所示:
let redirect = warp::path("segment").and(warp::path("another")).and(warp::path::end())
.map(|| {
warp::redirect(Uri::from_static("0.0.0.0:3030/segment/another/"))
});
然而这个重定向过滤器只匹配0.0.0.0:3030/segment/another/
。当我省略warp::path::end()
重定向适用于0.0.0.0:3030/segment/another
但现在一切(例如0.0.0.0:3030/segment/another/styles.css
) 被重定向到0.0.0.0:3030/segment/another/
.
有没有办法仅在路径不以斜杠或文件扩展名结尾时进行重定向(例如.html
, .css
etc.)?
可能我的整体方法在这里也不正确。