Redirect HTTP requests to HTTPS

Estimated reading time: 1 minute

This feature will make all the affected HTTP calls perform a redirect to its HTTPS counterpart before processing the call.

By default the redirection is a 301 Moved Permanently, but it can be configured to be a 302 Found redirect.

This feature is defined in the class io.ktor.features.HttpsRedirect and no additional artifacts are required.

Usage

fun Application.main() {
    install(HttpsRedirect) 
}

The code above installs the HttpsRedirect feature with the default configuration.

Configuration

fun Application.main() {
    install(HttpsRedirect) {
        // The port to redirect to. By default 443, the default HTTPS port. 
        sslPort = 443
        // 301 Moved Permanently, or 302 Found redirect.
        permanentRedirect = true
    }
}