Redirect HTTP requests to HTTPS

Estimated reading time: 1 minute

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

By default the redirection is a 301 Moved Permanently, but 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.


fun Application.main() {

The code above installs the HttpsRedirect feature with the default 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