How to do Site redirection using AWS S3?

Dilip Kola
Tensult Blogs
Published in
4 min readJun 27, 2018

This Blog has moved from Medium to blogs.tensult.com. All the latest content will be available there. Subscribe to our newsletter to stay updated.

What is site redirection? When we want to redirect a site “www.myolddomain.com” to “www.mynewdomain.com” then it is called site redirection and the URL in the user’s browser will also change with HTTP 301 status code, indicating that “www.myolddomain.com” is permanently moved to “www.mynewdomain.com”. This is usually done incase we want to rebrand our product with a new domain.

Site redirection can’t be done just using CNAME record at DNS server level, if we just use CNAME record to point “ www.myolddomain.com” to “www.mynewdomain.com” then URL won’t be redirected in the user’s browser and it will still show as “www.myolddomain.com” with the content from “www.mynewdomain.com”.

AWS S3 is famous for static web hosting and it also supports Site redirection so we don’t have to run any Nginx or Apache servers to handle site redirections.

Let us see how to do this using S3 and Route 53

  1. We need to create bucket with name “www.myolddomain.com” (replace myolddomain to your domain and select region appropriately)
Create S3 bucket using AWS S3 console

2. We need to step site redirection using “Static web hosting” feature of S3

Select bucket and Click on Properties
Click on Static website hosting
Set Redirection to new domain and select protocol appropriately

3. We can now test the redirection by clicking the link shown.

Click on the link marked to test redirection

4. Route 53 is an AWS DNS service which is guaranteed to be available 100% of the time and if you are using Route 53 for your domain then do the following, else you can set the above URL as CNAME in your domain using your domain registerer.

From Route 53 Console click Create Hosted Zone
Enter your old domain name and click Create

5. We need to create a DNS record for hosted zone on Route 53 for setting up redirection.

Select the appropriate Hosted Zone and click create Create Record Set

6. We need to create an A record for “www.myolddomain.com” for using Alias to point to S3 bucket which we created earlier.

Select S3 Website Endpoint and click Create

7. We also need to create an A record for myolddomain.com, as some users might be typing http://myolddomain.com. So redirection should apply for this address too.

Select www.myolddomain.com record set and click Create
All myolddomain.com Route53 DNS records

8. Now you can test by going to http://myolddomain.com and http://www.myolddomain.com, it should redirect to http://www.mynewdomain.com

What about HTTPS site URL redirection?

If you have noticed https://www.myolddomain.com won’t be redirecting to the new domain. Above process is sufficient if the site: myolddomain.com only supports HTTP but if we have to support HTTPS site URL redirection then we need to add two more component: CloudFront and ACM (AWS Certificate Manager).

S3 static web hosting doesn’t support HTTPS so in order to support HTTPS, we need to use CloudFront.

Create CloudFront distribution with S3 origin: www.myolddomain.com.s3-website.ap-south-1.amazonaws.com (S3 static website URL which we have configured earlier using S3 console).

Use this URL as origin in CloudFront

Please don’t follow the suggestion by CloudFront to choose S3 bucket URL while creating the origin. If you follow the suggestion, URL of the origin becomes: www.myolddomain.com.s3.amazonaws.com and site redirection won’t work properly with this origin URL. Also set Alternative domain names (CNAMEs) to www.myolddomain.com otherwise Route53 DNS forwarding doesn’t work.

CloudFront will ask for a SSL certificate as it is need for supporting HTTPS traffic so first we need to create a new or import an existing certificate to ACM then use it for CloudFront distribution. Please note that while selecting the SSL certificate, make sure it is associated with myolddomain.com otherwise you will get certificate errors; also make sure that ACM certificate is created in North Virginia (us-east-1) region as CloudFront allows certificates from that region only.

Once CloudFront distribution is created and then wait for it be deployed then update an A record in Route 53 for “www.myolddomain.com” with distribution domain name.

Now test HTTPS redirection by going to https://www.myolddomain.com.

This procedure only works for a single site redirection but if you want setup for multiple sites then I recommend you to go through the following blog:

Conclusion

We have learnt how to configure site redirection for both HTTP and HTTPS endpoints. Try it out and let me know if you have faced any issues, I will help you out.

--

--

Dilip Kola
Tensult Blogs

Spirtual Seeker | Mentor | Learner | Ex-Amazon | Ex-AWS | IIT Kanpur