This repository contains an example on how to remove trailing slashes when hosting a Hugo site on AWS S3 + CloudFront.
hugo >= 0.60
aws cli >= 1.16.287
- Correct AWS credentials configured on your machine (guide)
- Serves your static site without trailing slashes (
example.com/slash
will return the same content asexample.com/slash/
) - Redirects URLs with trailing slashes to URLs without trailing slashes
- Removes the trailing slashes from canonical links, stub pages and sitemap
- Redirects www to naked URL (
www.example.com
->example.com
) - Removes trailing slashes from sitemap, canonical links and page stubs
Note: This template assumes that you have your domain hosted on Route53. Feel free to remove the record set from the template and set your DNS-records in your preferred domain provider.
- Create ACM certificate, and make sure you select the option to add
www
to the same certificate upon creation. - Fill in all values in the
template.yaml
between the <> brackets, carefully noting there are a few <> in the Javascript code blocks. - Run the following command to deploy the bucket, CloudFront distribution and Route53 records needed to host your Hugo site:
aws cloudformation deploy --stack-name <STACK_NAME> --template-file template.yaml --capabilities CAPABILITY_IAM
To deploy your site to the bucket you just deployed run the following commands:
- Build files
hugo
- Sync files to S3
aws s3 sync ./public s3:https://<BUCKET_NAME> --delete --acl bucket-owner-full-control --acl public-read --cache-control max-age=31536000,public
- Invalidate the CloudFront cache
aws cloudfront create-invalidation --distribution-id <DISTRIBUTION_ID> --paths '/*'
- Done!