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.
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
Solution 5
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
Related videos on Youtube
![fredley](https://i.stack.imgur.com/bQZof.png?s=256&g=1)
Comments
-
fredley almost 2 years
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
todomain.com
, as currently this just returns a 404. This question on SO suggested aPTR
record, and I added that:www.domain.com PTR domain.com 300
but it didn't work. What should I be doing?
-
Ck- over 11 yearsThis 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.
-
Utpal - Ur Best Pal over 6 yearsI have done this however still it is not working. Anything else that may be required to be done?
-
trees_are_great almost 6 yearsAfter adding the alias, I needed to delete the browser cache to get this to work.
-
Hardeep Singh almost 6 yearsAnd it depends on the previous TTL of your DNS.
-
Madeo over 4 yearsnope, not working
-
Tuxedo Joe almost 4 yearsThis was exactly what I was looking for. Thank you!
-
myroslav over 3 yearsThis 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
-
Mathias Conradt over 2 yearsCNAME 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).
-
Admin about 2 yearswith "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
-
Admin about 2 yearsindeed 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