简体   繁体   English

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

[英]Automatic redirection to https with port change

Similar to this question , how can I change the port too? 与此问题类似,我也该如何更改端口?

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

For example, I want to change http://localhost:8080/test/ to https://localhost:8443/test/ , but right now it is only changing it to https://localhost:8080/test/ 例如,我想将http://localhost:8080/test/更改为https://localhost:8443/test/ ,但现在仅将其更改为https://localhost:8080/test/

Is there a way to change it without hardcoding it? 有没有一种方法可以在不对其进行硬编码的情况下进行更改? Currently I can hard code it like this window.location = "https:localhost:8443/test/" , but then I would have to do that for every unique page in test, and that could get to be a hassle. 目前,我可以像window.location = "https:localhost:8443/test/"这样对它进行硬编码,但是然后我必须为测试中的每个唯一页面执行此操作,这可能会很麻烦。

Instead, is there a way to automatically change the port? 相反,有没有一种方法可以自动更改端口?

You can reconstruct href: 您可以重建href:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM