Sitemap
Press enter or click to view image in full size

Handling Domain Redirection Using Amazon CloudFront, CloudFront Function, and S3

6 min readMay 22, 2025

--

Redirecting one domain to another is a classic enforcer move, whether you’re rebranding, migrating, or just keeping your SEO happy. At my work, even after months passed, along with numerous reminders (and a meme or two), our clients cling to our product’s old URL like it was their favourite coffee mug.

What’s a “Well-Meaning” engineer to do? Time for some Dev-magic!

In this article, I’ll show you how we gently nudged (okay, forcefully redirected 🙄) everyone to our new domain, using nothing but S3, CloudFront, and CloudFront Functions. No servers, no sweat, just pure cloud wizardry!

Why Use CloudFront, CloudFront Functions, and S3?

It’s cheap! And it rocks, but mostly, it’s cheap.

  • No servers (Serverless): No late-night patching or “did I reboot it?” moments.
  • Blazing fast: CloudFront’s edge locations ensure that users are redirected before they can say, “Wait, what happened?”
  • Cheap: S3 and CloudFront Functions are easy on the wallet and scale automatically.
  • Flexible: Easily handle single or multiple domains, custom paths, and HTTPS.

Prerequisites

Step 1: Create a New SSL Certificate in AWS Certificate Manager (ACM)

Before setting up your S3 bucket for redirection, provision an SSL/TLS certificate for your source domain using ACM. This ensures that all traffic-including HTTPS requests-can be securely redirected, which is essential for modern web standards and SEO.

  • Sign in to the AWS Management Console and go to the ACM console.
  • Click “Request a certificate.”
  • Click “Public Certificate”
Press enter or click to view image in full size
  • Add your source domain (e.g., olddomain.com). For broad coverage, consider also adding a wildcard (e.g., *.olddomain.com).
  • Select DNS validation (recommended for automation). ACM will provide a CNAME record to add to your DNS (typically in Route 53).
  • Choosing RSA 2048.
Press enter or click to view image in full size
  • While this is optional, it’s Good Practice to tag your resources
  • Click Request
  • Go to the AWS ACM console and view the details of your certificate.
  • ACM will provide CNAME records (name and value) to add to your domain’s DNS settings (Route53 TLD hosted zone).
Press enter or click to view image in full size
  • ACM will issue the certificate once validation is complete. This can take a few minutes to several hours.
  • When the status shows as “Issued,” your certificate is ready for use with CloudFront.
Press enter or click to view image in full size

Note: You can check if the CNAME record is correctly set up using commands like “dig” or “nslookup -type=CNAME CNAMEKEY”

Step 2: S3 Bucket Setup for Redirect

Create an S3 Bucket

  • Name the bucket however is best for remembering, but a recommended practice is naming the bucket exactly as your source domain (e.g., olddomain.com).
  • The bucket type should be General purpose
  • You can leave all settings as default
  • Click Create bucket

Configure for Redirection

  • In the S3 console, select your bucket.
  • Ensure that “Block all public access” is checked in the permissions tab.
Press enter or click to view image in full size
  • Go to the Properties tab.
  • Scroll down to Static website hosting.
  • Click “Edit” and Enable “Static Website Hosting”
  • Select Redirect requests for an object (not “Host a static website”).
  • Set the domain to your destination domain (e.g., newdomain.com).
  • Choose the appropriate protocol (https recommended).
  • Save changes.

After save, note the Bucket website endpoint on the static website hosting block, you’ll need this when setting up on CloudFront. It should look “http://{bucketname}.s3-website-{region}.amazonaws.com.”

This setup ensures that any request to the bucket results in an HTTP 301 redirect to the same path on the new domain

Step 3: Create and Configure the CloudFront Distribution

Create a CloudFront Distribution

  • Click “Create CloudFront Distribution”, Select Distribution Option based off your configuration Prefrences but for this purpose, we will be using “Single Website or App”
  • Set the origin domain to your S3 bucket’s static website endpoint (e.g., bucketname.com.s3-website-us-east-1.amazonaws.com).
  • Leave caching at default settings
  • Set Viewer Protocol Policy to “Redirect HTTP to HTTPS” for secure traffic.
  • Adjust other Configurations according to your needs
  • Add your source domain as an Alternate Domain Name (CNAME). (Not Optional)
  • Attach an SSL certificate for your domain (from AWS Certificate Manager).

Important: After creating the Distribution, copy the Distribution Domain name, navigate to “olddomain.com” on Route53, go into its Hosted Zone, create a new record, turn on Alias, and set it to “Alias for CloudFront Distribution and paste your DDN

Press enter or click to view image in full size

Step 4: Write and Attach a CloudFront Function

  • Navigate to the Functions feature on the CloudFront Distributions Console
  • Give a descriptive name to the Function and add a description
Press enter or click to view image in full size

Sample CloudFront Function:

Get Chidera Okoye’s stories in your inbox

Join Medium for free to get updates from this writer.

javascript

  • Add your Function Code to the Build Tab in the function page
  • Test your code to ensure functionality, and then publish
Press enter or click to view image in full size
Press enter or click to view image in full size
  • Deploy this function in the CloudFront console by creating an Associated Distribution.
  • Associate it with the Viewer Request event on your distribution.
Press enter or click to view image in full size

Step 5: Test the Redirect

  • Open your browser and navigate to the old domain.
  • You should be redirected instantly to the new domain, preserving the path.

Troubleshooting Tips:

  • Make sure DNS changes have propagated.
  • Double-check S3 bucket and CloudFront settings.
  • Use browser dev tools or curl -I to inspect redirect headers.

Advanced Tips and Use Cases

  • Multiple Domains: Repeat the setup for each domain you want to redirect.
  • Path-Based Redirects: Use CloudFront Function to map specific paths to new URLs.
  • SEO: HTTP 301 ensures search engines update their indexes.
  • HTTPS Everywhere: Always use SSL for secure, modern redirects.

Conclusion

With S3, CloudFront, and CloudFront Functions, you can implement robust, serverless domain redirects that scale globally and require zero maintenance. This approach is ideal for migrations, consolidations, and ensuring your users (and search engines) always find your content at the right place.

--

--

Chidera Okoye
Chidera Okoye

Written by Chidera Okoye

Permanently exhausted pigeon by nature. DevOps engineer by trade. Powered by pipelines, Integrations, and warm tea.