amazon-web-servicesamazon-s3amazon-cloudfronthttp-status-code-301

How do I 301 redirect a single page on my Cloudfront/ S3 hosted web page?


I use an AWS Cloudfront + S3 solution to host my website.

I redirect all naked domain and vanilla HTTP requests to the prefix https://www.example.com.

The following S3 Cloudfront Architecture cartoon captures my approach:

Cloudfront + S3 approach

In addition to the existing logic, I would like to also redirect a single page, namely:

https://www.example.com/category/coins/

to

https://www.example.com/cat/coins.html

I tried to do this via the S3 buckets redirect rules and received a 301 redirect to the bucket address, and not the cloudfront endpoint.

e.g.

http://www.example.com.s3-website-us-east-1.amazonaws.com/cat/coins.html

How can I correctly redirect this particular page?


Solution

  • AWS provides documentation on how to redirect with CloudFront and that are Viewer Request Functions.

    S3 also has documentation on how to do redirects based on different hosts