Properly redirecting is something that is easy to say (or request in a ticket) but not always easy to do. It just so happens that its really important for proper site behavior, particularly if you are concerned with SEO.
Here’s how you redirect a site with AWS in the simplest manner possible.
In this case things are pretty simple: you want either a 301 or a 302. A 301 is a permanent redirect, and a 302 is a temporary one. Of course, it’s your server so you can set a 301 redirection and then change it in the future, but the point is that you are signalling to others that the redirection is permanent, so they may stop checking the original URL and only catalog the ultimate one if you pick 301.
If you are redirecting an entire domain you definitely don’t want to keep a server running simply to handle those redirects. That would be expensive and wasteful. But you need a server-side HTTP code to get return to the browser. What’s the solution?
Basically what you want to do is use Route 53 to configure your DNS (technically any registrar could work, but it’s easier with Route 53 because it’ll be pre-configured) and then use S3 to handle the forward. S3 has built-in forwarding which will respond with a 301 response, despite there not actually being a server there that you control). Do to this you will actually be using S3’s website endpoint, so read up on that.
First you want to do the S3 work:
1. Create an S3 bucket that is named after your site, say WebSite.biz
2. Within the bucket, go to Properties > Static Website Hosting. Pick redirect and type in where you want the traffic to flow. Here’s where’d you type your new site, WebSiteTwo.io
Now in Route 53, you want to point your DNS from the old server to the new S3 bucket.
1. Go to your website and create a hosted zone.
2. Create an A record (IPv4)
3. Select “Yes” for Alias
4. For Alias Target, open the dropdown menu and look for your S3 website endpoint. Choose that. Now save. Don’t worry about Health Policy or anything else that might distract you.
5. Repeat steps 2-4 for your site’s www. record. Just keep in mind that the www. record will not be able to point directly at the website endpoint, unless you create an S3 bucket called “www.WebSiteTwo.io”. So you can create this bucket and forward it, or simply tell Route 53 to to another alias target in your record set, the one you just setup!
While the above method should work find for you, I actually prefer adding Cloudfront into the mix. This is pretty simple:
Now make sure you disable your old server so you don’t have to pay for it any more. You’ll also want to remove any caching distributions you might have had active. Those shouldn’t be a problem after the DNS is switched over, but could cause a lot of confusion down the line.
And that’s it! Give it five minutes and then test with a browser (make sure your cache is cleared), curl command, or online curl tool.
Sal Cangeloso November 24th, 2019
Posted In: AWS