개발/javascript

Javascript (자바스크립트) HTTP 접속 시 HTTPS로 리다이렉트 하는법

심집사 2021. 12. 2. 15:42

localhost가 아닌경우 http로 접속시 https로 리다이렉트

if(location.hostname != "localhost") {
    if (window.location.protocol != "https:") {
        window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
    }

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