50

I want to redirect all requests from example.com to www.example.com.

Preferably, this should happen at DNS level. I tried using PTR records, but that simply fails, returning a 404.

wwww.example.com is an ALIAS for an Elastic Load Balancer.

What’s the simplest way to achieve this?

TRiG
  • 1,193
  • 3
  • 14
  • 30
flavian
  • 655

5 Answers5

56

If you're already using Route 53, you can use their proprietary alias "record" to solve this problem. With standard DNS, you cannot do this at all and you have to have a web site send a 301 redirect. Of course, you still need to send the 301 redirects or deal with the fact that some requests will come in without the www (though you should send 301s for SEO reasons).

Probably the easiest way to do this is to set up an S3 bucket with the name of the naked domain and configure the bucket properties to redirect from example.com to www.example.com, and then in Route 53 create an alias for the naked domain name that points to that S3 bucket.

From the Comments

To enhance the answer, here is what we did to get this working:

  • Set up bucket - doesn't matter what its name is and must allow public.
  • In bucket, click properties and click static website hosting. Click redirect all requests to another host name and enter the site you want traffic to go to.
  • Copy the endpoint of the bucket name and go to the hosted zone in the Route53 console and add a CNAME with Alias No to the url that you need to be redirected from and paste the bucket endpoint as its value.
Michael Hampton
  • 252,907
7

For a DNS alias you simply need to add a CNAME or A record in DNS. See my answer here: How do I redirect www to non-www in Route53?

For URL-rewriting (as in redirecting the clients to another address) then you need some form of URL rewrite logic done at the webserver. There are several ways of doing this, and it depends on what kind of webserver you use. The most popular is using mod_rewrite with apache. This site is riddled with mod_rewrite questions, a few searches should get what you want.

pauska
  • 19,766
5

You can do this using the help of an s3 bucket. Steps are pasted below:

Step 1. Create an s3 bucket.

Step 2. Make the bucket as a Static website hosting with redirect.

Step 3. Edit AWS Route 53 entry for non www domain.

A – IPv4 Address -> Alias Yes -> Alias Target (Select the s3 bucket end point from the drop down).

Step 4. Save record set.

womble
  • 98,245
0

If your website is stored in an S3 bucket you can consider following the following step:

  1. Create two S3 buckets with name exact domain name (example.com and www.example.com)
  2. Enable bucket policy for public access
  3. Enable a static website for both domains, however, we will config redirect for example.com.
  4. Create two CloudFront for the two S3 buckets Create two alias A records for example.com and www.example.com and route them to the two CloudFront respectively.

For more detail, you can follow the document: Host a static website on amazon s3 and CloudFront

Chris
  • 1
0

AWS Route53 doesn't currently provide support to redirect what's currently known as an APEX record (the root domain) to another domain name, although they're working at a solution.

I've achieved this by creating an A (alias) record which points my root domain (devopsfolks.com) to the IP address of my WP hosted solution...And the answer is yes, if the IP changes then you'll need to change the alias. However the provider guaranteed me that the IP is fixed unless they need to perform some sorts of disaster recovery and change the hardware in which case they will put a proxy together to perform the redirections while giving the customers 45 days to change their mapping.

Marco Tedone
  • 101
  • 1