Tuesday, 6 June 2017

Naked domain redirection over HTTPS for an OpenShift Node.js app

I created an OpenShift app using Node.js Auto-Updating cartridge (Node.js v7, NPM v4).

I've set up a custom domain (say example.com) with an SSL certificate.

I'm using the free naked domain redirection offered by wwwizer.com.

I would like all requests to be redirected to https://www.example.com.

Here is my server (using Express) that forces redirection to HTTPS (as explained in OpenShift FAQ):

express()

  .use((request, response, next) => ("http" === request.headers["x-forwarded-proto"])
    ? response.redirect(`https://${request.headers.host}${request.path}`)
    : next())

  // ...

  .listen(process.env.NODE_PORT || 3000, process.env.NODE_IP || "localhost", () => {
    console.log("Application started :)");
  });

This works quite well, except for one case:

  • http://www.example.com: redirects to https://www.example.com;
  • http://example.com: redirects to https://www.example.com;
  • https://www.example.com: stays on https://www.example.com;
  • https://example.com: fails to load, net::ERR_CONNECTION_REFUSED on Chrome.

Any idea how to handle this last case?



via sp00m

No comments:

Post a Comment