How to redirect domains using Amazon Web Services

Written by Adrian Holovaty on October 11, 2013

Yesterday I saw some people online referring to my site Soundslice (a guitar tabs site) as "Songslice," so I registered songslice.com, with the intention of redirecting it to soundslice.com for the benefit of anybody who typed in the wrong domain.

In the past, I've done this sort of redirect at the web server level (nginx, Apache). But now that I'm on the nice Amazon infrastructure, I'm not actually using one of those servers -- requests are handled directly by gunicorn once they make it through Amazon's load balancers. So I needed to find another way of handling the redirect.

One option would be to tweak my application (using Django middleware) to detect requests to songslice.com and redirect them, but it seemed dirty to do it directly in my app.

Another option would be to use the domain-redirecting service provided by my domain registrar (Namecheap), but registrars don't always use proper 301 redirects, and I've gotten burned in the past by shoddy performance/downtime of registrar-provided services.

That left me with the option of doing the redirects directly within the Amazon Web Services world. I couldn't find a single document that explained how to do this, so here are my notes on how to set up a domain-level redirect with the current AWS tools. (Note that Amazon is adding new stuff seemingly constantly, so this may be out of date in a few years or months!)

The one-sentence explanation: point the new domain at S3 and use S3's "redirect all requests" feature.

Step 1: Create S3 bucket for domain

Go to the S3 console and create a bucket. For the name, use the exact, full domain, e.g., "songslice.com". (This is important!)

Once you've created it, edit its properties. In the "Static Web Hosting" section, select "Redirect all requests to another host name." In that section, enter the domain name to redirect to, e.g., "soundslice.com".

Step 2: Add DNS rules in Route 53

Go to the Route 53 console and create a Hosted Zone. The domain name should be your new domain name, e.g., "songslice.com".

Once you've created it, click "Go to Record Sets." You should see two records already ("NS" and "SOA").

Click "Create Record Set" and make sure the Type is "A". Leave the Name field blank, to indicate you're dealing with no subdomain. Change Alias to "Yes". Click in the Alias Target field, and you should see a dropdown including an "S3 Website Endpoints" section. Select the S3 bucket you created in the previous step. Save the record set.

Step 3: Give registrar Amazon's nameservers

Log into your account on the domain registrar's site (e.g., Namecheap), for the domain that you want to redirect (e.g., songslice.com). Find the place where you can change the nameservers for the domain. You might have to check something that says "Stop using registrar's nameservers."

Enter the four Amazon nameservers for your new domain. They're listed in the Route 53 record set you created in step 2 (next to "NS"). Copy and paste them from Route 53 into your registrar's nameserver UI.

Step 4 (optional): Do it again for www subdomain

If you want to support redirects for people typing in a www subdomain (I recommend this), repeat steps 1 and 2 using, e.g., "www.songslice.com" instead of "songslice.com".

The only difference is: in Route 53, instead of creating a new record set for the www version, edit the one you already created and add another "A" record. This time, type "www" in the Name field when you add it.

Final thoughts

That's it! Depending on DNS, this could take a little while (a few hours?) to propagate across the Internet. Oh, and this will cost you around a dollar per month, for the S3 and Route 53 services, and there's nothing to maintain.

Comments

Posted by Dugald Holmes on November 15, 2013, at 4:58 p.m.:

Hey Adrian, just thanking you for your article as it ended up being the best way for us to redirect our users from www.myinfoq.com and our mobile optimised site m.myinfoq.com to their new homes at www.rememberitfor.me and m.rememberitfor.me and this made life a lot easier.
Cheers, Dugald.
CTO, RememberItFor.me

Comments have been turned off for this page.