Reputation: 1463
if i use:
<meta http-equiv="REFRESH" content="0;url=https://www.the-domain-you-want-to-redirect-to.com/index.html">
in the html code then it will endlessly loop and refresh the https page.
How can i redirect the users to https? [regarding one index.html file]
What do i need to put in the html code of that "index.html" to redirect them, if they use only "http"?
Thanks
Upvotes: 19
Views: 47424
Reputation: 295
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.
Upvotes: 2
Reputation: 101604
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.
Upvotes: 50