开发者

automatic redirection to https?

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 开发者_如何学Ccan 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜