rust - 如何在扭曲中使用斜杠重定向到 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;
所以现在
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
?
可能我的整体方法在这里也不正确。
解决方案
推荐阅读
- debugging - NS3_Can't run vanet-routing-compare.cc
- python - sankey 图的绘图为空
- javascript - Rails 部分运行 JS
- php - Livewire 在尝试对 ... 组件进行水合时遇到损坏的数据
- sql - 如何从 oracle 触发器更改为 oracle 复合触发器?
- reactjs - 动态添加元素时,状态保持在该元素内的默认值
- pentaho-spoon - 区分连字符 [-] 和负号 [-2.22]
- r - 如何自动计数()每个变量
- java - 使用 LibGdx openURI 时出现错误 -12
- python - 在 Python 中使用部分旋转的高斯消除