首页 > 解决方案 > 如何在扭曲中使用斜杠重定向到 URL?

问题描述

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;

所以现在

原则上我不会介意这一点,只是总是使用带有斜杠的 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

可能我的整体方法在这里也不正确。

标签: rustrouteswebserverurl-routingrust-warp

解决方案


推荐阅读