https://kotlinlang.org logo
#ktor
Title
# ktor
s

spierce7

07/27/2019, 10:17 PM
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

Big Chungus

07/28/2019, 10:55 AM
You'd be better off using status pages feature and configuring it to handle all 404 exceptions with redirects to main site
s

spierce7

07/28/2019, 6:35 PM
that's a really good idea. Thanks! I didn't realize the status pages feature was so flexible