47

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,167
  • 2
  • 13
  • 30
flavian
  • 625
  • 1
  • 6
  • 9
  • What did you try with PTR records? I'm curious. – TRiG Feb 18 '14 at 14:25
  • @flavian the poster asked for a DNS rewrite, not a URL rewrite, and he specifically said that he did not care how the URL looked like. – pauska Feb 18 '14 at 14:36
  • 4
    @flavian for f sake, you said that you want to do the redirect in DNS. My answer explains how to do it in DNS. If you want to redirect the URL then you need a URL rewrite. It has nothing to do with DNS. – pauska Feb 18 '14 at 14:40
  • Is there any particular reason for doing that? I'd do it [the other way around](http://no-www.org/). – nyuszika7h Feb 18 '14 at 19:43
  • SANs in EV SSL. – flavian Feb 18 '14 at 19:47
  • For anyone looking to do this as part of a static hosted site on S3, Amazon has a great tutorial that covers this process in great detail: https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html – Julian Jan 22 '16 at 23:51
  • Does anyone know of an authoritative resource for doing this in conjunction with CloudFront? – pdoherty926 Sep 21 '18 at 23:33

4 Answers4

55

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
  • 237,123
  • 42
  • 477
  • 940
  • 1
    @michael-hampton correct me if I'm wrong but the S3 bucket only redirects for the naked domain name with no path, right? I added an S3 bucket correctly for my domain, but it only redirects the naked domain, and does not redirect anything that has a path. I'm guessing mod rewrite is the way to go? – John Harding Oct 13 '16 at 00:53
  • @JohnHarding I dunno. I don't use most Amazon AWS services anymore, including S3. – Michael Hampton Oct 13 '16 at 01:44
  • 2
    To enhance the answer, here is what we did to get this working: - Set up bucket - doesn't matter what it's 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. – NathanQ Oct 02 '17 at 17:58
  • 7
    This only works for http requests, as S3 does not support https. Requests to `https://example.com` will fail. – Harry King Mar 07 '18 at 14:33
  • to make `https://example.com` redirect to `https://www.example.com` you sadly need to setup another cloudfront pointing on a bucket with static http redirect to the `www.example.com`. It's worth mentioning that you need to add the apex domain into the `CNAME` field in your cloudfront configuration – tuhaj Feb 17 '19 at 04:49
  • I found this aws documentation helpful. Same steps as this answer, but it goes into a bit more detail: https://aws.amazon.com/premiumsupport/knowledge-center/redirect-domain-route-53/ – Daniel Long Jul 14 '20 at 14:01
  • @JohnHarding I have the same behaviour. Did you find a solution? Thanks – Learner Feb 05 '22 at 07:47
  • @Learner I can't remember specifically for all paths, but for a recent project I needed one path to work and used the `Redirection rules` section. Here is AWS documentation on it: https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-page-redirect.html – John Harding Feb 07 '22 at 05:18
  • @JohnHarding Thanks John. The `redirection rules` section is not visible on the new S3 web console. In John's link, there is instructions from Amazon on this matter. – Learner Feb 09 '22 at 00:04
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,532
  • 4
  • 55
  • 75
  • I used CNAME on my internal bind9 srv exactly an hour ago, but results are not the same as with url rewrite. When entering this CNAME into the browser address bar, you get exactly that URL, it doesn't change to the url which CNAME points to. I'm using apache mod_rewrite now, sending 301s when there's no www. prefix. – Kitet Feb 18 '14 at 16:49
  • 3
    [The answer you link to](https://serverfault.com/a/454978/148410) is for sending `www.domain.com` to `domain.com`. This question is about the opposite. Does your solution of using `CNAME` or `A` records work for sending `domain.com` to `www.domain.com`? – Carl G Aug 20 '17 at 19:38
  • @CarlG Your root `domain.com` cannot have a CNAME record – DollarAkshay Mar 13 '21 at 08:11
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
  • 95,029
  • 29
  • 173
  • 228
Arunlal Ashok
  • 111
  • 2
  • 4
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