Let us say that you do not want to www. subdomain for your website and want to have a 301 redirect to non-www version. There are millions of way of doing that, but luckily you are on AWS, you serve your domain via Route53. You can then keep your application configuration clean from this redirect logic via native AWS tools: You can leverage S3 and Cloudfront to accomplish this goal.
Following the infrastructure as code principle, I do not paste here complicated steps or screenshots on AWS console, but here I share a Cloudformation template that will set up everything for you:
You should just install this Cloudformation template to us-east-1 region (It will check where it is installed and fail if you install to another region.) The only parameters that it needs are domain name without www and Hosted Zone Id in Route53 to create necessary record.
This template will create a SSL certificate for you via Amazon Certificate Manager, create a S3 bucket with required redirection logic, a Cloudfront distribution to wrap all of them and you will have your serverless redirection ready in minutes.
Note that for certificate creation, AWS will send an confirmation E-Mail to your domain’s registered E-Mail address and Cloudformation will not be installed before you approve the E-Mail. Also as you might already know, Cloudfront updates take time so please give this template some time to be fully installed.
Happy cloud computing!