Skip to content

Instantly share code, notes, and snippets.

@khalby786
Created June 3, 2020 15:45
Show Gist options
  • Save khalby786/a9311ac535c8c7f42b9ec0f5d922f49c to your computer and use it in GitHub Desktop.
Save khalby786/a9311ac535c8c7f42b9ec0f5d922f49c to your computer and use it in GitHub Desktop.
HTTP ----> HTTPS Redirecting

Using client-side Vanilla JS in your browser

// https://gist.github.com/youngchief-btw/d8c327ef245f2e9998997a41c7b34e70
if (location.protocol === "http:") {
  location.replace(window.location.href.replace("http:", "https:"));
}

Use Express for Node.js

function checkHttps(req, res, next){
  // protocol check, if http, redirect to https
  
  if(req.get('X-Forwarded-Proto').indexOf("https")!=-1){
    console.log("https, yo")
    return next()
  } else {
    console.log("just http")
    res.redirect('https://' + req.hostname + req.url);
  }
}

app.all('*', checkHttps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment