37

I host my site at domain.com.

My DNS entries in Route53 are as follows:

domain.com      A       xxx.xxx.xxx.xxx      300
domain.com      NS      stuff.awsdns-47.org  172800
domain.com      SOA     stuff.awsdns-47.org  900

I would like to redirect traffic from www.domain.com to domain.com, as currently this just returns a 404. This question on SO suggested a PTR record, and I added that:

www.domain.com  PTR     domain.com           300

but it didn't work. What should I be doing?

3
  • 4
    If you actually want to send a HTTP redirect, you can't do it inside DNS. If you don't care about the URL in the address bar changing, use a CNAME as pauska suggests. If you do care, you will need a web server of some sort to send the 301. Some DNS providers have this as a service, but I don't believe Route53 does this.
    – cjc
    Dec 4, 2012 at 16:33
  • @cjc I don't care about the URL in the address bar.
    – fredley
    Dec 4, 2012 at 16:35
  • For other readers using Cloudflare, this worked for me: community.cloudflare.com/t/redirecting-www-to-non-www/2949/28
    – Venryx
    Aug 30, 2021 at 12:40

7 Answers 7

29

PTR is for setting up reverse IP lookups, and it's not something you should care about. Remove it.

What you need is a CNAME for www:

www.domain.com  CNAME  domain.com 300
5
  • 22
    This is not a redirect. In case, you have to define callback URL in some sort of third party service, above solution will not work. For example oAuth(Facebook, Twitter) Authentication.
    – Ck-
    Mar 13, 2013 at 9:32
  • 2
    nope, not working
    – Madeo
    Feb 9, 2020 at 13:30
  • CNAME would mean you have duplicate content on www and Apex, which isn't optimal for Google/SEO and gets a penalized (in SEO terms, for duplicate content). Nov 2, 2021 at 11:26
  • @MathiasConradt ..what would you recommend then. Nov 12, 2022 at 12:17
  • @user1388835 Doing a real redirect, as mentioned in the first comment of the original question, or first comment on here as well. Nov 20, 2022 at 11:08
19

You can also set a ALIAS for WWW to A record of domain.com:

www.domain.com A ALIAS domain.com 300

so your final DNS entries would be as follows:

domain.com          A       xxx.xxx.xxx.xxx      300
domain.com          NS      stuff.awsdns-47.org  172800
domain.com          SOA     stuff.awsdns-47.org  900
www.domain.com      A       ALIAS domain.com (Hosted Zone ID)
3
  • I have done this however still it is not working. Anything else that may be required to be done? Feb 14, 2018 at 9:32
  • After adding the alias, I needed to delete the browser cache to get this to work. Sep 4, 2018 at 8:44
  • And it depends on the previous TTL of your DNS. Sep 5, 2018 at 10:17
8

I was able to get this set up by leveraging an additional S3 bucket.

I want my website to be accessible at the non-www example.com. In my case, example.com is set up with a route 53 hosted zone, a s3 bucket, and a cloudfront distribution with a custom ssl cert.

I want www.example.com to redirect to example.com. To do so, I set up a new s3 bucket for www.example.com. I set it to public and set up static website hosting to redirect all requests. The target bucket or domain is example.com. Since I have the ssl cert configured on example.com, I set the protocol to https.

enter image description here

In route 53, within the mydomain.com hosted zone, I created a new A record for www.example.com with an Alias that pointed to the new www s3 bucket website.

Now all requests to www.example.com redirect to https://example.com.

Hope this helps.

3
  • This was exactly what I was looking for. Thank you!
    – Tuxedo Joe
    Aug 22, 2020 at 7:53
  • 3
    This solution is ok for HTTP but is not working for HTTPS requests. You need to involve CloudFront to serve HTTPS. See serverfault.com/a/942537/7005
    – myroslav
    Nov 27, 2020 at 21:50
  • indeed with this method you can only redirect TO https but not FROM https, you need to do the extra step via cloudfront as in the comment linked above
    – hansaplast
    Apr 25, 2022 at 14:25
3

After you have a CNAME for both example.com and www.example.com this nginx config will redirect traffic from http to https as well as all www.example.com to example.com

server { 

    listen  80 ;

    server_name  example.com, www.example.com;

    rewrite ^/(.*) https://example.com/$1 permanent;
}

server {  #  redirect www to normal domain

    listen       443  ssl ;

    server_name www.example.com;

    include /etc/nginx/myprojname/include/ssl;

    return 301 https://example.com$request_uri;
}

server {

    listen  443 ssl ;

    include /etc/nginx/myprojname/include/ssl;

    server_name example.com;

    include /etc/nginx/snippets/nginx_common_location_443;

    location / {

        proxy_pass http://127.0.0.1:3000/;
    }

    include /etc/nginx/myprojname/include/custom_server_include;
}

where my actual server is up and listening on port 3000 ... this also terminates my TLS however just remove mention of ssl ... tucked away in those included files are my nginx settings to harden the box

2

As mentioned above, it's not possible with standard DNS.

Here's the solution I used:

  • Setup S3 with static website redirect to your non-www domain
  • Create a Cloudfront distibution to your S3 (using the S3 domain, not the one suggested in autocomplete by AWS)
  • Add a Route 53 A Record alias to the CloudFront distribution
3
  • @Thomas see edited answer.
    – natanavra
    Dec 3, 2018 at 8:59
  • with "not the autocomplete" what is meant is that you need to copy the static website url and paste it into the origin dialogue of cloudfront. E.g. www.example.com.s3-website.eu-central-1.amazonaws.com
    – hansaplast
    Apr 25, 2022 at 14:18
  • didn't work for me
    – Bersan
    Dec 10, 2022 at 22:51
0

After meddle up with lots of online tutorials, I found it was very easy without the need for any service other than Route53.

This is the example configuration for the domain with www,

enter image description here

This is an example configuration for the same domain without www,

enter image description here

So basically you have to copy the same configuration including the IP address again from www.domain.com to domain.com.

And that's it wait for some time to get your domain changes to propagate and you'll be able to see your website loading both in the domain with and without www.

1
  • 2
    This is not what the OP asks for, the desire is a "redirect", which this is not.
    – Automatico
    Nov 25, 2021 at 8:40
0

It depends on how you are serving the site. The easest way is probably serve through cloudfront. That way you would simpley create the cloudfront distribution and add both the www and non-www domain as alias'. Cloudfront will take care of both.

1
  • how do you "add both www and non-www domains as alias" in cloud front? that's the question
    – Bersan
    Dec 10, 2022 at 22:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .