4

If I want to redirect http://url > https:/url like: http://example.com?param=value#bookmark > https://example.com?param=value#bookmark

I see how I can redirect with javascript but the method leaves out the ?params and #bookmark.

What's a good way to redirect and retain the whole URL?

Clay Nichols
  • 11,090
  • 25
  • 101
  • 162
  • A better solution might be to just configure a redirect at the domain level. – dwjohnston Aug 04 '18 at 03:47
  • 1
    Possible duplicate of [Detect HTTP or HTTPS then force HTTPS in JavaScript](https://stackoverflow.com/questions/4723213/detect-http-or-https-then-force-https-in-javascript) – yotke Aug 04 '18 at 07:44

1 Answers1

2

Try this:

if (window.location.protocol == "http:") 
    window.location.href = "https" + window.location.href.slice(4);

or just replace

if (location.protocol == 'http:')
  location.href = location.href.replace(/^http:/, 'https:')
Hossein
  • 2,650
  • 3
  • 12
  • 29