no-www snippets

How to configure a no-www redirection on Amazon AWS (S3 and Cloudfront)

Tagged no-www, amazon, s3, cloudfront  Languages bash

How do you configure a site hosted on Amazon AWS, e.g. a static website, to redirect from "www" to the "no-www" domain (aka "naked domain")? The tool for the job is the AWS CLI.

For example, this creates a redirection from www.x.com to x.com for a bucket named www.x.com:

aws s3api put-bucket-website --bucket www.x.com --website-configuration '{
    "RedirectAllRequestsTo": {
        "HostName": "x.com"
    }
}'

Use this command to check that the configuration is set properly:

aws s3api get-bucket-website --bucket www.x.com