How To Redirect a WWW Subdomain Name To a Root Domain With Amazon S3
Alex Paul Migit
Elite Life Coach & Business Advisor | Sr. Data & Analytics Engineer | Athlete | Accredited Investor | 10X | Empire Builder | Founder & Serial Entrepreneur | Musician & Singer-Songwriter | The WLS Foundation 501(c)(3)
Want to redirect a www subdomain name to a root domain?
You can easily redirect a www subdomain name to a root (apex) domain using Amazon S3 and Amazon Route 53 following the instructions below.
Requirements
- A hosted zone for the domain yourdomain.com that is hosted in Amazon Route 53.
- You have permissions to add resource records to the hosted zone of yourdomain.com.
- You have permissions to create an Amazon Simple Storage Service (Amazon S3) bucket.
- You are able to create an S3 bucket with the exact name for www.yourdomain.com.
Unlike typical DNS record sets with hosting providers like GoDaddy and BlueHost, for example, the www subdomain will be outside of the hosted zone for your primary domain (e.g. yourdomain.com) in AWS, so you will not be able to create a CNAME Record Set in Route 53 for this.
Login to your AWS Management Console and Navigate to your Amazon S3 Storage Service
Under Buckets section, click Create bucket.
Bucket name should be www.yourdomain.com and Region the same as the Region you set up your EC2 Instance. Click Next.
You can leave Configure options as-is, unless there are requirements for you to fulfill here.
Click Next, and un-check Block all public access. Check I acknowledge that the current settings may result in this bucket and the objects within becoming public.
Review and Create your bucket
Select the www.yourdomain.com bucket that you just created.
Click Properties tab and select Static website hosting.
Select Redirect requests radio button and enter yourdomain.com under Target bucket or domain.
Enter http under Protocol and click Save.
Navigate to the Route 53 service, and select your hosted zone (e.g. yourdomain.com)
Click Create Record Set and enter www ["." will prefix domain automatically] for the subdomain in the Name section.
Leave A - IPv4 address type and select Yes radio button to use an Alias.
Next to Alias Target, select the blank value field, expand the drop-down menu, and choose the S3 bucket (www.yourdomain.com) you just created under the heading S3 Website Endpoints.
Verify that the Routing Policy is set to Simple.
Next to Evaluate Health Target, verify that No is selected and choose Create.
The www subdomain should now be redirecting traffic to your primary domain!
You can confirm this by browsing to www.yourdomain.com or CURL to www.yourdomain.com. You should see "301 Moved Permanently" in your curl output, redirecting to yourdomain.com.
[<USER>@<PRIVATEDNS> ~]$ sudo yum install curl Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.cat.pdx.edu * epel: mirrors.kernel.org * extras: mirrors.cat.pdx.edu * remi-php72: repo1.sea.innoscale.net * remi-safe: repo1.sea.innoscale.net * updates: mirrors.cat.pdx.edu Package curl-7.29.0-54.el7_7.1.x86_64 already installed and latest version Nothing to do [<USER>@<PRIVATEDNS> ~]$ curl www.yourdomain.com <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>
If you haven't already created the A Record for yourdomain.com, do so with either your IPv4 Public IP for the EC2 Instance hosting yourdomain.com or Elastic Load Balancer (ELB) Alias Target.
If you are using a load balancer in your VPC: I recommend configuring your internet-facing ELB with a Dual-Stack endpoint.
You will want to enable IPv6 support by using the DNS name with the dualstack prefix to ensure that clients can access the load balancer using either IPv4 or IPv6.
Example: dualstack.name-123456789.region.elb.amazonaws.com
Hope this helps! If you have any questions, tips or suggestions, please feel free to reach out to me or leave a comment below.
Chief Marketing Officer | Product MVP Expert | Cyber Security Enthusiast | @ GITEX DUBAI in October
2 年Alex, thanks for sharing!