I have a problem with paths. I want to make a redi...
# ktor
g
I have a problem with paths. I want to make a redirect from
/path
to ‘/path/’ because the HTML returned by ‘/path/’ has some relative paths for images, css, … But when I declare theses 2 paths in
routing
, ktor seems to make no difference between them.
o
trailing slash is ignored by routing, but that’s a compelling reason to stop doing so…
g
yes… but browsers make difference between URL with trailing slash and without. Ktor should also, no?
o
I think yes, but not very clear how exactly
g
Just
/path
and
/path/
should be considered as distinct.
On https://github.com/ktorio/ktor/blob/8f4f43a0b0281b213f0be1df1b5fa8b160ae1e5c/ktor-server/ktor-server-core/src/io/ktor/routing/RoutingPath.kt#L10 the filtering
it.isNotEmpty()
removes the last empty segments that appears in case of trailing slash.
o
Not only that.
Copy code
routing {
   route("foo") {
      route("bar") {
         // what here to make a distinction?
      }
   }
}
g
Ok, I understand the problem. Will think about it.
We have to find a solution because it’s a pity to add a proxy server to manage this kind of redirection.
o
Absolutely. For now you can workaround by looking at the original path
if (!call.request.path().endsWith('/')) return@get call.respondRedirect("…")
g
I don’t understand where this code would go.
That is the code I wanted to use (and not working)
Copy code
routing {
            get("/") { call.respond(call.resolveResource("index.html")!!) }
            get("/federerInData")   { call.respondRedirect("/federerInData/", permanent = true) }
            get("/federerInData/")  { call.respond(call.resolveResource("/federerInData/index.html")!!) }
o
instead of two routes for
federerInData
, use one (either) and put that code at top
g
👍