首页 > 解决方案 > 重定向到 Ktor 中的绝对 URL

问题描述

我正在学习如何使用 ktor。对于特殊用例,我需要从我的 ktor 服务器重定向到不同的域。但是我现在不能让它正常工作。

作为一个简单的例子,我有一个 Application.kt

import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

fun main(args: Array<String>) {
    val server = embeddedServer(Netty, port = 8080) {
        routing {
         get("/") {
           call.respondRedirect("www.google.com")
        }}
    }
}
server.start(wait = true)

}

我除了它把我重定向到www.google.com但是它把我重定向到localhost:8080/www.google.com

标签: kotlinktor

解决方案


弄清楚了。您还需要设置协议。这有效

call.respondRedirect("https://google.com/")

推荐阅读