DDDD
DDDD

Reputation: 3940

Check web page url and make https if entered as http

I have a web page that is routed with https and all works well. I noticed I could change it to http and the page still loaded.

I am trying to check the window url and make it https if it was entered as http. Only for this page and with Jquery or Javascript. ( I know most would recommend not using a script for security )

This isn't working for me:

<script>
var url = window.location.pathname;
if url.match('^http://')
{
url = url.replace(/^http:\/\//, 'https://');
    window.location.pathname = url;
}

Thank you

Upvotes: 2

Views: 2454

Answers (1)

Avitus
Avitus

Reputation: 15958

If you really want to do it client side:

<script>

   if (  window.location.protocol != 'https:' ) {
           window.location = document.URL.replace("http://","https://");
    }

</script>

Upvotes: 2

Related Questions