Redirecting www Domain to non-www using 0 code at AWS

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!

Cagatay Gurturk’s Blog

My personal blog where I share stuff about IT, mostly about…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store