How do I redirect www to non-www in Route53?

Solution 1:

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

Solution 2:

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)

Solution 3:

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.


Solution 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