35

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?

fredley
  • 555
  • 1
  • 5
  • 14
  • 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 04 '12 at 16:33
  • @cjc I don't care about the URL in the address bar. – fredley Dec 04 '12 at 16:35
  • For other readers using Cloudflare, this worked for me: https://community.cloudflare.com/t/redirecting-www-to-non-www/2949/28 – Venryx Aug 30 '21 at 12:40

7 Answers7

30

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
pauska
  • 19,532
  • 4
  • 55
  • 75
  • 20
    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 '13 at 09:32
  • 1
    nope, not working – Madeo Feb 09 '20 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). – Mathias Conradt Nov 02 '21 at 11:26
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)
Hardeep Singh
  • 374
  • 4
  • 11
7

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.

Federico
  • 171
  • 1
  • 2
  • This was exactly what I was looking for. Thank you! – Tuxedo Joe Aug 22 '20 at 07:53
  • 2
    This solution is ok for HTTP but is not working for HTTPS requests. You need to involve CloudFront to serve HTTPS. See https://serverfault.com/a/942537/7005 – myroslav Nov 27 '20 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 '22 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

Scott Stensland
  • 225
  • 4
  • 10
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
natanavra
  • 129
  • 4
  • @Thomas see edited answer. – natanavra Dec 03 '18 at 08: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 '22 at 14:18
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.

Stranger
  • 103
  • 3
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.

Kevin
  • 101