How to redirect to URL with trailing slash in warp?

752 Views Asked by At

I am using warp to serve a directory of static files. Unfortunately the relative links used in those static files can only be resolved, when I add a trailing slash to my path.

This is the code I use to serve the directory:

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/ works fine
  • 0.0.0.0:3030/segment/another does not work

In principle I would not mind this and just always use the URLs with trailing slashes, however when I "Add to Homescreen" the page from Safari on iOS (as a PWA) the trailing slash is ommited automatically.

So in order to work around this, I tried to create a redirect, and then add the trailing slash like this:

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/"))
});

however this redirect filter only matches 0.0.0.0:3030/segment/another/. When I omit the warp::path::end() the redirect works for 0.0.0.0:3030/segment/another but now everything (for example 0.0.0.0:3030/segment/another/styles.css) gets redirected to 0.0.0.0:3030/segment/another/.

Is there a way to only redirect when the path does not end with a slash or a file extension (e.g. .html, .cssetc.)?

It might be that my overall aproach is incorrect here, too.

0

There are 0 best solutions below