Easily redirect HTTP and HTTPS traffic at WWW.[domain.tld] to the root of the domain [domain.tld].
This repository provides an AWS CloudFormation Template to construct a CloudFront SSL/HTTPS endpoint, an S3 bucket for redirection and correct Route53 DNS entries.
You can download the automated script for this repo and run it yourself for your deployment:
curl -s https://raw.githubusercontent.com/cbschuld/aws-cf-redirect-www-to-root/main/create-redirect-www-to-root.sh > /tmp/create-redirect-www-to-root.sh && bash /tmp/create-redirect-www-to-root.sh && rm /tmp/create-redirect-www-to-root.sh
You will need the following, which are all automated but good to understand what assets you need to stand up in the stack.
Determine the zone ID using the AWS CLI. In this example I'll use my named profile example
and look for example.com
Please note you'll need jq
for this operation to work. If you are on MacOS, for example, you can add it with brew: brew install jq
#!/bin/zsh
# Prompt for user input
echo "Enter AWS CLI Profile Name:"
read profile
echo "Enter Domain:"
read domain
# Fetch HostedZoneID
aws route53 list-hosted-zones-by-name --profile=$profile |
jq --arg name $domain \
-r '.HostedZones | .[] | select(.Name=="\($name)") | .Id'
/hostedzone/Z1UVA2VESUQ1UN
aws cloudformation create-stack --stack-name www-redirect --template-body file://redirect-www-to-root.yml \
--parameters \
ParameterKey=DomainName,ParameterValue=example.com \
ParameterKey=HostedZoneID,ParameterValue=Z1UVA2VESUQ1UN \
--region=us-east-1 \
--profile=example