DEV Community

Boryamba
Boryamba

Posted on • Updated on

AWS Elastic Beanstalk: redirect http to https (and www to non-www)

Table of content:

Hello everyone.

In previous parts we created dockerized NextJS app, deployed it to AWS EB and connected our app to custom domain.

In this part we are going to set up redirects from http to https (for security reasons) and from www to non-www (for SEO reasons).

Everything in here is within AWS Free tier.

Also please pay attention that all links to AWS I provide use my region of choice, if you're using different AWS region make sure to change it before making any changes!

Setting up redirects

To set up redirects we need to open EC2 Load Balancers Console, select our balancer and open Listeners tab.

We will start with redirecting http to https:

  1. In HTTP listener row Rules column click View/edit rules
  2. Switch to Edit rules mode - open Pen tab
  3. Click on Pen icon on the left of HTTP 80: default action
  4. Delete default action in THEN column
  5. Click Add action -> Redirect to
  6. Enter port 443 on the right of protocol dropdown and click check icon to save changes
  7. Click Update. edit mode save changes

Next we will redirect www to non-www:

  1. Switch to Add rules mode
  2. Click Insert rule
  3. IF column select Host header
  4. Inside is field enter www.yourwebsite
  5. THEN column click Add action -> Redirect to
  6. Enter port 443 and save changes add mode save changes

We also want to set redirection from www to non-www on https connections:

  1. Go back to Listeners tab
  2. Click View/edit rules in HTTPS row
  3. Add new rule:
    1. IF host header is www.yourdomain
    2. THEN redirect to
    3. Set port 443
    4. Change Origin host, path, query to Custom
    5. Replace value of Host field with your website domain
  4. Save changes. add new https rule

You now have fully functional https website that is being served completely free of charge (within Free tier quota of course).

Hope you liked the series. Bye-bye =)

Top comments (0)