automatic redirection to https?

This could be more elegant

if(/https/.test(window.location.protocol)){
    window.location.href = window.location.href.replace('http:', 'https:');    
}

However, this is an answer in a code level. To answer the question a bit more broadly, it is even possible to not even touch the code for that, like in the network level or in the application level.

In the network level, you can use an edge router like Traefik. In the application level, you can use a reverse proxy for the redirection. Reverse proxies like Ngnix or services like Cloudflare or AWS Cloudfront.

Also note that in case you host your website on platforms like Firebase Hosting, you will have the automatic redirection to https by default.


var loc = window.location.href+'';
if (loc.indexOf('http://')==0){
    window.location.href = loc.replace('http://','https://');
}

maybe? as long as you don't mind a small javascript dependency.

Tags:

Html