HTTP to HTTPS redirection on App Engine Flexible

前端 未结 3 800
傲寒
傲寒 2020-11-29 09:40

I\'ve followed the answer of this: Redirect from http to https in google cloud but it does not seem to be currently accurate any more. The anchor referenced ( https://cloud.

3条回答
  •  抹茶落季
    2020-11-29 09:54

    Pulling Justin's yes-https library, I was able to get this to work:

    var app = express();
    app.use(function(req, res, next){
      if (req.host != 'localhost' && req.get('X-Forwarded-Proto') == 'http') {
        res.redirect(`https://${req.host}${req.url}`);
        return;
      }
    
      app.router(req, res, next);
    });
    

    At first I thought I had to do that since I was on an appengine subdomain and couldn't use HSTS. Then I learned HSTS works fine for subdomains. :) Regardless, I thought people might want to see what the magic bit to use was if they didn't want to use yes-https for some reason.

    Justin, auto-redirecting all traffic to SSL by default sounds great to me. I just spent hours trying to figure out how to do so before I found this post because I was trying to get my app to get Chrome's add to homescreen install banner as per https://developers.google.com/web/fundamentals/engage-and-retain/app-install-banners/.

提交回复
热议问题