0

Updated: Just use .htaccess please, if you're using JavaScript there's something 100% wrong with your code.

Cooper
  • 47
  • 2
  • 10
  • from http://example.com > http://www.example.com with Javascript. or from http://example.com > https://www.example.com with Javascript?? – Koushik Chatterjee Jul 10 '17 at 20:20
  • 1
    You should not rely on JavaScript for this. Using `.htaccess` or a server side language (such as PHP or similar) can give you the proper 301 permanent redirect. See this [related question](https://stackoverflow.com/q/13846738/248567). The web browser will show a certificate mismatch error before the browser can execute the JavaScript redirection. SSL certificates should typically include both www. and non-www. – Kirk Beard Jul 10 '17 at 20:20
  • Why don't you fix the certificate? They're free from Let's Encrypt, and virtually any vendor will give you a dual www and non-www certificate if you ask. – ceejayoz Jul 10 '17 at 20:22
  • 1
    Correct, if you have a certificate problem your javascript will not load even in your browser – Koushik Chatterjee Jul 10 '17 at 20:22

1 Answers1

0
window.location.href = window.location.protocol + "//www." + window.location.host

look at the location object for more info: https://developer.mozilla.org/en-US/docs/Web/API/Location

Our_Benefactors
  • 3,220
  • 3
  • 21
  • 27