Supporting HTTPS URL redirection with a single CloudFront distribution

You’re thinking too narrowly — there’s nothing wrong with this setup.

The solution would be trivial if Amazon offered any form of URL rewriting

They do — the empty bucket.

S3 has absolutely no support for HTTPS.

Not for web site hosted buckets, no… but CloudFront does.

CloudFront is not just a CDN. It’s also an SSL offloader, Host: header rewriter, path prepender, geolocator, georestrictor, secure content gateway, http to https redirector, error page customizer, root page substituter, web application firewall, origin header injector, dynamic content gzipper, path-based multi-origin http request router, viewer platform identifier, DDoS mitigator, zone apex alias target… so don’t get too hung up on “CDN” or on the fact that you’re stacking one service in front of another — CloudFront was designed, in large part, to complement S3. They each specialize in certain facets of storage and delivery.

So, you did it right… most of it, anyway… Create a bucket, configure it for web site hosting, set it to redirect all requests to another site (the non-www) and put a CloudFront distribution in front of it — using the web site endpoint URL for with bucket in CloudFront, not the one from the drop-down list — configured with high TTLs so that CloudFront will send a minimal number of requests to S3 then put your (free!) SSL certificate from Amazon Certificate Manager. HTTPS alternate domain routing: solved. No servers, no troubleshooting, and cheap. The only charges are the usage — there is no background recurring charge as there would be with servers.

Extra credit: configure the redirecting CloudFront distribution for the cheapest rate tier. Redirects from more expensive locations will either be routed to a cheaper edge location or — at CloudFront’s option — may be served out of a higher cost location but billed at the lower rate.

Note that most of the time, CloudFront should serve the redirects from S3 from it’s cache… and when you configure a bucket to redirect all requests to another hostname, the redirect is a 301 permanent redirect — which browsers are supposed to cache, themselves.

Leave a Comment