skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    I used the following to redirect certain pages:

    
    [
        {
            "Condition": {
                "KeyPrefixEquals": "category/data-science"
            },
            "Redirect": {
                "HostName": "john.soban.ski",
                "Protocol": "https",
                "ReplaceKeyWith": "cat/data-science.html"
            }
        },
        {
            "Condition": {
                "KeyPrefixEquals": "category/coins"
            },
            "Redirect": {
                "HostName": "john.soban.ski",
                "Protocol": "https",
                "ReplaceKeyWith": "cat/coins.html"
            }
        },
        {
            "Condition": {
                "KeyPrefixEquals": "category/ietf"
            },
            "Redirect": {
                "HostName": "john.soban.ski",
                "Protocol": "https",
                "ReplaceKeyWith": "cat/ietf.html"
            }
        },
        {
            "Condition": {
                "KeyPrefixEquals": "category/howto"
            },
            "Redirect": {
                "HostName": "john.soban.ski",
                "Protocol": "https",
                "ReplaceKeyWith": "cat/howto.html"
            }
        }
    ]
    

  2. 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

    • Configure Host name in S3 (S3 -> Properties -> Static website hosting -> Host name). This may rewrite the domain part (I’m not 100% sure)
    • Advanced redirection rules allow to replace paths with different keys which should definitely work. There is an example on the page that fits the question
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search