通过端口更改自动重定向到 https

Automatic redirection to https with port change

本文关键字:重定向 https      更新时间:2023-09-26

与这个问题类似,我也可以如何更改端口?

if (window.location.href.indexOf('http://')==0)
          window.location=window.location.href.replace('http://','https://');

例如,我想将http://localhost:8080/test/更改为https://localhost:8443/test/,但现在仅将其更改为https://localhost:8080/test/

有没有办法在不硬编码的情况下更改它?目前我可以像这样硬编码它window.location = "https:localhost:8443/test/",但是我必须为测试中的每个唯一页面执行此操作,这可能会很麻烦。

相反,有没有办法自动更改端口?

你可以重建 href:

if (window.location.href.indexOf('http://')==0)
    window.location = 'https://'+window.location.hostname+':8443'+window.location.pathname+window.location.search;