vercel / serve

Static file serving and directory listing
https://npmjs.com/package/serve
MIT License
9.39k stars 693 forks source link

Redirect all traffic from http to https #597

Open romrayster opened 4 years ago

romrayster commented 4 years ago

Hello, I would like to redirect all http traffic to https and I am using an instance of create-react-app Currently, I have two instances of serve running:

serve -s /root/build/   --ssl-cert  /etc/letsencrypt/live/sub.example.com/cert.pem    --ssl-key   /etc/letsencrypt/live/sub.example.com/privkey.pem -n -l tcp://sub.example.com:443   
serve -s /root/build/   -n -l tcp://sub.example.com:80 

(sub.example.com is, of course, replaced by my applet domain) Both pages can be opened respectively with http and https My serve.json:

{
    "redirects": [
        {
            "source": "http://sub.example.com/:path",
            "destination": "https://sub.example.com/:path",
            "type": 301
        },
                {
            "source": "http://sub.example.com/",
            "destination": "https://sub.example.com/",
            "type": 301
        },
                {
            "source": "http://sub.example.com/subpath",
            "destination": "https://sub.example.com/subpath",
            "type": 301
        },
                {
            "source": "http://sub.example.com/**",
            "destination": "https://sub.example.com/",
            "type": 301
        }
    ]
}

None of the redirects seem to work. The serve version is 11.3.0 Any ideas on how to approach the issue?

romrayster commented 4 years ago

For those who stumble on a similar issue, a workaround would be to use this module: https://github.com/mbasso/react-https-redirect which redirects on the client side. Due to a lack of solution I've switched to nginx, would be curious though if the redirect is theoreitcally possible with serve

ShashankaNataraj commented 3 years ago

@romrayster Is it possible to share your nginx config? Im trying to do the exact same thing..

JohnForster commented 3 years ago

It would be really nice to be able to drop a "redirectToHttps": true or "forceHttps": true into serve.json and have this happen automatically.

warren-bank commented 2 years ago

shameless self-promotion alert:
this feature is available in my @warren-bank/serve fork of serve

see: the --force-https option

alumbo commented 1 year ago

You could use redirection from http to https with the configuration file serve.json


{
  "redirects": [
    {
      "source": "/(.*)/?",
      "destination": "https://yourdomainhere.com/$1",
      "type": 301
    }
  ]
}
Mayoyo25 commented 1 year ago

Use this: { "headers": [ { "source": "/(.*)", "headers": [ { "key": "Strict-Transport-Security", "value": "max-age=63072000; includeSubDomains; preload" } ] } ] }

urslimshady commented 1 year ago

You could use redirection from http to https with the configuration file serve.json

{
  "redirects": [
    {
      "source": "/(.*)/?",
      "destination": "https://yourdomainhere.com/$1",
      "type": 301
    }
  ]
}

This one worked fine for me