Redirect to the www subdomain using AWS.
How to redirect your top level domain to the www
subdomain, using Amazon Web Services.
In order to redirect requests to your top level domain to the www
subdomain, you’ll need a web sever to respond with the redirect whenever it receives a request to the top level domain:
→ GET https://example.net/eating
← 301/302
location: https://www.example.net/eating
The easiest way to do this on AWS is to use the static website hosting feature on S3.
- Create an S3bucket with the same name as your top level domain name — here we’ll use
mydomain.net
. - Enable static website hosting on that bucket.
3. Set up the redirection. For this example, to take care of doing 302
and redirecting to https
:
Include the following XML in the ‘Redirection rules’ box (changing mydomain.net to be your own domain, of course).
<RoutingRules>
<RoutingRule>
<Redirect>
<Protocol>https</Protocol>
<HostName>www.mydomain.net</HostName>
<HttpRedirectCode>302</HttpRedirectCode>
</Redirect>
</RoutingRule>
</RoutingRules>
Side note: You could use the simpler “Redirect requests” option (which will 301
all requests to the location you specify.) But there are two caveats with doing this:
- you may wish to 302
(at least initially until you’re sure things are set up properly — as it’s less permanent)
- it’s probably a good idea to redirect everything to https
(which I’m assuming your www.mydomain.net
domain can support in this day and age).
4. Set up a Cloudfront distribution to proxy the requests through to S3.
Enter the endpoint for the static S3 website (you can get this by clicking into the static website hosting dialog you were in earlier in S3) as the ‘Origin Domain Name’.
5. Lastly, point your DNS at the Cloudfront distribution.
Go to the A
record for the top level domain and point it at your Cloudfront distribution. If you click the magnifying glass icon you should see your Cloudfront distribution in the list.
I hope this article is helpful to you. If any of the steps are unclear let me know in the comments — I’m happy to update it.