Reputation: 3940
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
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