Search code examples
expressnginxamazon-ec2amazon-elastic-beanstalkamazon-route53

Redirect non-www to www with aws elastic beanstalk


I'm using Elastic Beanstalk and I've followed the instructions to deploy my app using the express web server as follow: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html

This setup uses nginx and route 53.

Everything works well, but now I'm trying to redirect from non-www/non-https URLs to "https://www.domain.com" (always https with www).

I've seen different solutions out there that either aren't working or seem hacky. What's the proper way to do this from the aws console?

Thanks a lot!


Solution

  • You can setup a S3 bucket that redirects naked domain to www. It is explained here.

    http://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html

    You can redirect http to https by using Cloudfront. You can read more information here.

    http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-https.html

    You can setup the webserver on your EC2 instances to redirect as well, but that requires that you setup up your SSL certificate as well. It is easier to let AWS handle that with Cloudfront.

    You are probably using Apache so it would be something like this.

    NameVirtualHost *:80
    <VirtualHost *:80>
       ServerName mysite.example.com
       DocumentRoot /usr/local/apache2/htdocs 
       Redirect permanent / https://mysite.example.com/
    </VirtualHost>
    
    <VirtualHost _default_:443>
       ServerName mysite.example.com
       DocumentRoot /app/directory/
       SSLEngine On
       # etc...
    </VirtualHost>
    

    Then setup your SSL certificate with LetsEncrypt in your deploy script.