27

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 '12 at 16:33
  • @cjc I don't care about the URL in the address bar.
    – fredley
    Dec 4 '12 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 at 12:40
25

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
2
  • 18
    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 9:32
  • nope, not working
    – Madeo
    Feb 9 '20 at 13:30
17

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 '18 at 9:32
  • After adding the alias, I needed to delete the browser cache to get this to work. Sep 4 '18 at 8:44
  • And it depends on the previous TTL of your DNS. Sep 5 '18 at 10:17
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.

2
  • This was exactly what I was looking for. Thank you!
    – Tuxedo Joe
    Aug 22 '20 at 7:53
  • 1
    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 '20 at 21:50
4

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

1

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
1
  • @Thomas see edited answer.
    – natanavra
    Dec 3 '18 at 8:59
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.