Redirect www to non-www and HTTP to https in Elasticbeanstalk Route53 Application Load Balancer
Elasticbeantalk is a great service to deploy your app with minimum setup and you get app auto-scale out of the box without a need set up your own application load balancer from scratch.
In a web application, there is always a need to choose a domain as your primary domain for example in this case I choose example.com as the primary domain and www.example.com as an alternative domain and then redirect every request from www.example.com to example.com to avoid duplicate content or canonicalizing URL in every page.
301( permanent) redirect is an HTTP status code
As 301 redirection is not part of DNS but it happens at Http level, Some DNS providers offer a solution to solve this in DNS management, for instance, GoDaddy DNS management has a Forwarding feature to solve this issue.
This is not the case for Amazon Route53. For AWS there are a couple of solutions to solve this:
1. Use S3 + Cloudfront to capture the domain and redirect it to the primary domain. I found this very hard to set up and troubleshoot because of the nature of Cloudfront CDN caching.
2. Modify the application server to redirect to the primary domain. I don’t like this solution too since it requires modifying the application server (puma, Nginx,..) especially for a managed container like Elasticbeanstalk — it could break anytime for future release.
3. Using an ALB rule. This is the most efficient way in my opinion. It is easy to troubleshoot if things don’t go right, you don’t need to use other services to do it (S3, Cloudfront), your setting will not break in the future update of your application server.
Setting up ALB rule for redirection
In EC2 console, on the left panel select “Load Balancers”, It will show a list of your application load balancers. Select your application load balancer you want to modify.
If you have many application load balancers, the easier way to identify which one is your target you can check in the Tag tab -> key/value to identify your resources as shown below.
Click on the “Listeners” tab and then choose View/Edit rule in HTTP 80 row to redirect http://www.example.com.
and then insert a new Rule.
Fill in the following:
- Host header: www.example.com
- Redirect to: Https
- Choose: Custom host ( we want to redirect to a host)
- Host: example.com
And keep everything like in the figure above and then Save.
Now you can test your redirection with the following command:
curl -Is http://www.example.com
Output
HTTP/1.1 301 Moved PermanentlyServer: awselb/2.0Date: Wed, 19 Jun 2019 15:44:14 GMTContent-Type: text/htmlContent-Length: 150Connection: keep-aliveLocation: https://example.com:443/
Now we need to follow the same procedure for https://www.example.com
Insert a new rule
Now you can test your redirection with the following command:
curl -Is https://www.example.com
Output
HTTP/1.1 301 Moved PermanentlyServer: awselb/2.0Date: Wed, 19 Jun 2019 15:48:10 GMTContent-Type: text/htmlContent-Length: 150Connection: keep-aliveLocation: https://example.com:443/
Conclusion
Redirection seems to be straightforward in other DNS providers — you can set redirection/forwarding in their DNS management tool easily and conveniently, however, AWS tries to solve this with different approach separating it completely from DNS making it hard to discover and even sometimes you have to use other AWS services to solve the issue.
The Amazon way is a bit complicated, but once you clear about the concept you will appreciate amazon way better for separating things to its own building block. It will be difficult for those who wanna start building things quickly without prior experiences if you compare with other players in the market like Godaddy, Heroku or Digital ocean.