I'm trying to create a redirect, so that all bad u...
# ktor
s
I'm trying to create a redirect, so that all bad urls redirect the user back to my main site. I've noticed that this ends up interfering with my
static
block for some image requests (i.e.
/favicon-32x32.png
) aren't found and get returned with the redirect, but when I comment out the
redirectUnknown
function, it works fine. It's odd because my
static
block is placed before
redirectUnknown
. Does anyone know of a better way to do this?
Copy code
fun Route.redirectUnknown() {
    // Unknown path - redirect back to main site
    get("*") {
        val domain = call.request.domain()
        val port: String = run {
            val port = call.request.port()
            if (port == 80 || port == 443) "" else ":$port"
        }

        val redirectUrl: String = if (BuildUtil.isDev) {
            val scheme = call.request.origin.scheme

            if (domain.value == "localhost" || domain.isIpAddress()) {
                "$scheme://$domain$port/"
            } else {
                "$<scheme://www>.$domain$port/"
            }
        } else {
            "<https://www>.$domain$port/"
        }

        call.respondRedirect(redirectUrl)
    }
}
b
You'd be better off using status pages feature and configuring it to handle all 404 exceptions with redirects to main site
s
that's a really good idea. Thanks! I didn't realize the status pages feature was so flexible