AWS Elastic Beanstalk: redirect http to https (and www to non-www)
Tags: nextjs, aws, docker, beanstalk
Published: 2021-08-16 16:24:40

##### Table of content:
- [Create dockerized NextJS app](https://dev.to/bnn1/deploying-dockerized-nextjs-app-to-aws-beanstalk-with-custom-domain-3hpk) - part 1
- [Deploy dockerized app to AWS EB](https://dev.to/bnn1/deploying-dockerized-nextjs-app-to-aws-eb-part-2-deployment-to-beanstalk-47pj) - part 2
- [Connect custom domain to our application](https://dev.to/bnn1/deploying-dockerized-nextjs-app-to-aws-eb-part-3-setting-custom-domain-45bm) - previous part

Hello everyone.

In previous parts we [created dockerized NextJS app](https://dev.to/bnn1/deploying-dockerized-nextjs-app-to-aws-beanstalk-with-custom-domain-3hpk), [deployed it to AWS EB](https://dev.to/bnn1/deploying-dockerized-nextjs-app-to-aws-eb-part-2-deployment-to-beanstalk-47pj) and [connected our app to custom domain](https://dev.to/bnn1/deploying-dockerized-nextjs-app-to-aws-eb-part-3-setting-custom-domain-45bm).   

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](https://eu-west-1.console.aws.amazon.com/ec2/v2/home?region=eu-west-1#LoadBalancers:sort=loadBalancerName), 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`
1. Switch to `Edit rules` mode - open Pen tab
1. Click on Pen icon on the left of `HTTP 80: default action`
1. Delete default action in `THEN` column 
1. Click `Add action` -> `Redirect to`
1. Enter port `443` on the right of protocol dropdown and click check icon to save changes
1. Click `Update`.
![edit mode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1x0ov5t3o2x1e5ppzhw.png)
![save changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xr7ein42pjgy62yyfiev.png)  

Next we will redirect www to non-www:
1. Switch to `Add rules` mode
1. Click `Insert rule`
1. `IF` column select `Host header`
1. Inside `is` field enter `www.yourwebsite`
1. `THEN` column click `Add action` -> `Redirect to`
1. Enter port 443 and save changes
![add mode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/la4ki6j0v9y83lfy28ko.png)
![save changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/howxdi284aqw7tx78cwx.png)

We also want to set redirection from www to non-www on https connections:
1. Go back to Listeners tab
1. Click `View/edit rules` in HTTPS row
1. Add new rule:
    1. `IF` host header is `www.yourdomain`
    1. `THEN` redirect to
    1. Set port 443
    1. Change `Origin host, path, query` to `Custom`
    1. Replace value of `Host` field with your website domain
1. Save changes.
![add new https rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11k4mzo7zmj1i2tei8ca.png)

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 =)