var myurl ="https :// io..hsoub..com"; if( (myurl.substring(0,5)=="https") && (location.protocol == "http :") ){ //redirect to https as @ Khaled Html 's example }else if( (myurl.substring(0,4)=="http") && (location.protocol == "https :") ){ //redirect to http }