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 301 redirect a site with AWS S3 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.
For a bit of added confusion, S3 can do 301 redirects but it cannot add SSL (HTTPS) on its own, so you need to use Cloudfront as well.
If you already know your way around AWS, here is what we are going to to do.
Here at the full, start from fresh, instructions for 301ing a S3 domain. First, do the AWS S3 work:
Now in Route 53, you want to point your DNS from the old server to the new S3 bucket.
Note: Endpoints will not appear immediately after they are created. They can take up to 15 minutes to show up!
While the above method should work find for you, I actually prefer adding Cloudfront into the mix. This is mandatory if you want HTTPS URLs. The good news is that this is cheap and 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 January 19th, 2020
Posted In: AWS