Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting HTTP url to HTTPS using regular expression and javascript

I'm in a situation where I need to rewrite an url in javascript and switch it from http protocol to https.

I can match https urls with:

if(url.match('^http://')){

but how do I form the https url using regular expressions and javascript?

url =  "https://" + ?;
like image 802
stevebot Avatar asked Mar 30 '11 19:03

stevebot


2 Answers

Replace directly with a regex :

url = url.replace(/^http:\/\//i, 'https://');
like image 174
Stephan Avatar answered Oct 16 '22 08:10

Stephan


Cannot it be done by simply replacing the http string?

if(url.match('^http://')){
     url = url.replace("http://","https://")
}
like image 26
MPękalski Avatar answered Oct 16 '22 07:10

MPękalski