Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudfront redirects to S3 endpoint

  1. I've set up a cloudfront distribution using Endpoint address.
  2. I want to redirect calls to the /Latest folder to the /12.50 folder.
  3. So I defined a redirect rule in the S3 console, using the routing rules markup.

routing rule

Result: the redirect kicks in, but moves the user from the cloudfront URL to the endpoint URL.

e.g. A user who comes in at <mysite>.cloudfront.net/Latest gets redirected to <bucket>.hpe-alm.s3-website-us-west-2.amazonaws.com/12.50.

Question: is there a way for the redirect to keep the user on the cloudfront domain?

like image 403
Reuven Fine Avatar asked Oct 30 '22 04:10

Reuven Fine


1 Answers

Rather than performing the redirect within Amazon S3, you could change the origin path within CloudFront.

  • Define an origin with an origin path of /12.50
  • Edit the behaviour of the CloudFront distribution when you want it to point to a different version

Here's some examples:

Full redirect

In this case, I re-pointed the root (dxxx.cloudfront.net) to a sub-directory:

  • I created an Amazon S3 bucket and applied a bucket policy to make it all publicly readable
  • I created two folders in the bucket: v1 and v2
  • I uploaded two different pictures to each folder, but with the same filename
  • I created an Amazon CloudFront distribution two Origins, pointing to v1 and v2 respectively
  • I set the distribution's default behaviour to point to the v1 origin
  • Once the distribution was ready, I accessed dxxx.cloudfront.net/picture.jpg. It returned the picture from v1.
  • I then modified the behaviour to point to the v2 origin. Once the distribution was ready, I accessed the same URL and it returned the picture from v2.

Redirect of a sub-directory

In this example, I re-pointed dxxx.cloudfront.net/Latest to a different sub-directory.

The important difference is that the destination subdirectory has to contain a /Latest subdirectory, since it is actually pointing to dxxx.cloudfront.net/<version>/Latest, with only the <version> portion being substituted.

  • To the above example, I added a /Latest folder within the v1 and v2 subdirectories and put pictures in each of them
  • I added an extra behaviour with a Path Pattern of /Latest/* pointing to the v1 origin
  • Once the distribution was ready, I accessed dxxx.cloudfront.net/Latest/picture.jpg. It returned the picture from /Latest/v1.
  • I then modified the /Latest behaviour to point to the v2 origin. Once the distribution was ready, I accessed the same URL and it returned the picture from /Latest/v2.
like image 146
John Rotenstein Avatar answered Dec 28 '22 12:12

John Rotenstein