https://github.com/cbschuld/aws-cf-redirect-www-to-root
Redirect all HTTP and HTTPS traffic from the www.[domain.tld] to [domain.tld] (www redirector)
https://github.com/cbschuld/aws-cf-redirect-www-to-root
Last synced: 9 months ago
JSON representation
Redirect all HTTP and HTTPS traffic from the www.[domain.tld] to [domain.tld] (www redirector)
- Host: GitHub
- URL: https://github.com/cbschuld/aws-cf-redirect-www-to-root
- Owner: cbschuld
- License: mit
- Created: 2024-04-01T22:51:23.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-02T00:11:32.000Z (over 1 year ago)
- Last Synced: 2025-01-25T11:26:05.396Z (11 months ago)
- Language: Shell
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Redirect HTTP/HTTPS Traffic from WWW to the Domain Root
Easily redirect HTTP and HTTPS traffic at WWW.[domain.tld] to the root of the domain [domain.tld].
## Using CloudFront, S3 and Route53
This repository provides an AWS CloudFormation Template to construct a CloudFront SSL/HTTPS endpoint, an S3 bucket for redirection and correct Route53 DNS entries.
## Automated Usage
You can download the automated script for this repo and run it yourself for your deployment:
```sh
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
```
## Prerequisites for the Manual Usage
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 Hosted Zone ID
Determine the zone ID using the AWS CLI. In this example I'll use my named profile `example` and look for `example.com`
#### Using the AWS CLI
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`
```sh
#!/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'
```
#### Example output:
```
/hostedzone/Z1UVA2VESUQ1UN
```
## Manual Usage of the Stack Template
```
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
```