I own foo.com
and bar.com
. I am managing both in Route53. foo.com
hosts my site, and I'd like to direct traffic from bar.com
to foo.com
. I tried to set up a CNAME
record for bar.com
pointing to foo.com
, but I got the error message:
RRSet of type CNAME with DNS name foo.com. is not permitted at apex in zone bar.com.
Why doesn't this work, and what can I do instead?
As per RFC1912 section 2.4:
A CNAME record is not allowed to coexist with any other data. In
other words, if suzy.podunk.xx is an alias for sue.podunk.xx, you
can't also have an MX record for suzy.podunk.edu, or an A record, or
even a TXT record. Especially do not try to combine CNAMEs and NS
records like this!:
podunk.xx. IN NS ns1
IN NS ns2
IN CNAME mary
mary IN A
The RFC makes perfect sense as the nameserver wouldn't know whether it needs to follow the CNAME or answer with the actual record the CNAME overlaps with. bar.com
is a zone therefore it implicitly has an SOA record for the bar.com
name. You can't have both a SOA record and a CNAME with the same name.
However, given that SOA records are generally used only for zone maintenance, these situations where you want to provide a CNAME at the zone's apex are quite common. Even though the RFC prohibits it, many engineers would like a behaviour such as: "follow the CNAME unless the query explicitly asks for the SOA record". That's why Route 53 provides alias records
. These are a Route 53 specific feature which offer the exact functionality you require. Have a look at http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/CreatingAliasRRSets.html
- Create an S3 Bucket called
bar.com
. (The name must be the same as the domain you want to redirect from in order for this to work!)
- In the
bar.com
S3 Bucket go to Properties
> Static Website Hosting
, select Redirect all requests to another host name
and enter foo.com
in the text box.
- Back in Route 53, in your
Hosted Zone
for bar.com
, click Create Record Set
. Select A - IPv4 address
for type. Click Yes
for Alias
. Click the text box for Alias Target
. bar.com
should be listed under -- S3 Website Endpoints --
. Save the record. Wait a few minutes and you should have a redirect setup to redirect requests from bar.com to foo.com.
You can use this same method to redirect a naked domain to a subdomain (like www). I use this in cases where www.foo.com has to be a CNAME so I redirect from foo.com to www.foo.com with this same method. If foo.com is an A record, you can use this technique to redirect from www.foo.com to foo.com.
NOTE: this method will forward with the full path. i.e. http://bar.com/test will forward to http://foo.com/test.
On Route53, You need to create an A record NOT a CNAME record, and create an alias under that.
From @ewalshe's comment on Alexandru Cucu's answer,
if you came here trying to setup API Gateway with a custom domain name and have a Cloudfront distribution url.
You should use the DNAME instead of a CNAME.
A CNAME record is only able to redirect the label to another label.
When you talk about redirecting domain names instead of labels, you should use DNAME
$ORIGIN bar.com
IN DNAME foo.com
This also means that every A, NS and any more records should be deleted. This have to be configured in the foo.com domain.
tldr; You have to pass in an FQDN as the ResourceRecordSet name.
I had this same problem using this statement c# snip:
private static void RegisterHostWithDns(IAmazonRoute53 ec2,SynoviaImage image)
{
var changeBatch = new ChangeBatch();
var rRs = new ResourceRecordSet(image.Name, RRType.CNAME) {TTL=60,ResourceRecords = new List<ResourceRecord>() { new ResourceRecord(image.PublicDns)} };
var change = new Change(ChangeAction.UPSERT, rRs);
changeBatch.Changes.Add(change);
var request = new ChangeResourceRecordSetsRequest(ConfigurationManager.AppSettings["DnsZoneId"], changeBatch);
var response = ec2.ChangeResourceRecordSets(request);
Console.WriteLine("Updated CNAME For {0} setting {1}",image.Name,image.PublicDns);
}
In this case image.Name == "Listener"
Once I changed it to:
private static void RegisterHostWithDns(IAmazonRoute53 ec2,SynoviaImage image)
{
var changeBatch = new ChangeBatch();
var rRs = new ResourceRecordSet(string.Format("{0}.{1}",image.Name, "testing.foo.bar.com"), RRType.CNAME) {TTL=60,ResourceRecords = new List<ResourceRecord>() { new ResourceRecord(image.PublicDns)} };
var change = new Change(ChangeAction.UPSERT, rRs);
changeBatch.Changes.Add(change);
var request = new ChangeResourceRecordSetsRequest(ConfigurationManager.AppSettings["DnsZoneId"], changeBatch);
var response = ec2.ChangeResourceRecordSets(request);
Console.WriteLine("Updated CNAME For {0} setting {1}",image.Name,image.PublicDns);
}
now the value being passed in is: "Listener.fully.qualified.com"
It works now.