18

What is the difference between a CNAME and a Subdomain?

I understand that the a cname (the left side of a domain) can point to the domain, so you can two different urls point to the same address, ie.

ex1.mydomain.com - if setup as a CNAME can return the IP of mydomain.com

If ex1.mydomain.com is setup as a subdomain, does it have a different IP?

Another question is what should the ideal setup be in this situation:

I have IP1:80 for a web app

I have IP2:80 for another app

Can I point both of these IPs to the same A record, with perhaps a different cname or subdomain?

Thanks for any help?

2 Answers 2

33

A CNAME is a type of DNS record, where a hostname points at another hostname.

An A record is another type of DNS record, where a hostname points at an IP address.

A subdomain is what you described as 'the left side of the domain', e.g. webmasters.stackexchange.com is a subdomain of stackexchange.com. The DNS setup for a subdomain could use either an A record or a CNAME.

Your question:

Can I point both of these IPs to the same A record, with perhaps a different cname or subdomain?

doesn't really make sense. You don't point IPs at A records, you point hostnames at IPs using A records. If you're asking if you could point a domain and a subdomain at the same IP, the answer is yes.

This might be clearer with a real world example:

webmasters.stackexchange.com has an A record that points to the IP 198.252.206.140. stackexchange.com also has an A record that points to the IP 198.252.206.140.

It would therefore be possible to change webmasters.stackexchange.com to CNAME to stackexchange.com, and everything would continue to work as it does now.

(In practice, CNAMES are slightly slower than A records as they might result in an additional DNS lookup, so that's one reason why A records are more commonly used.)

1
  • Thanks for the awesome answer! I had originally asked this question in stackoverflow, and when I was moving it over here, it got answered there as well. My solution is going to be to use two two different A records, one as a subdomain, so I can use the same domain. IE - ex1.mydomain.com (IP1) and mydomain.com (IP2)
    – password
    Apr 1, 2014 at 17:57
-3

See comments for updated info -- This comment is most likely incorrect.

The answer above is not exactly accurate - if the the A Records were forwarded to the IP and the IP had virtual hosts setup they would be directed to different locations on the same server IP.

Changing it to a CNAME would most likely break this.

5
  • 2
    I'm pretty sure that CNAME records and virtual hosts work just fine together. Apr 20, 2020 at 12:51
  • Whether you use an A or CNAME record in DNS is irrelevant in terms of how the request is ultimately resolved on the destination server. Whatever DNS records are used - they are just a way to find the required IP address for the server you need to connect to. When the HTTP request is ultimately made to the server, whatever DNS lookups have been required to find the server's IP address are irrelevant and completely "hidden" from the destination server.
    – MrWhite
    Apr 30, 2020 at 16:11
  • Thanks for the clarification guys - I hadn't tested it. My concern was that if sub.domain.com is a different site and the cname redirects it to domain.com you would get the wrong site. If I understand correctly, CNAME just uses the dns name to find the ip.
    – Ces
    May 15, 2020 at 22:12
  • @Ces CNAME records don't redirect hostnames on the HTTP level, they only direct traffic on the IP level. If sub.example.com is CNAMEd to example.com, the requests for sub.example.com get directed to the same IP address that example.com is pointed at, but the requests don't get rewritten to ask for content from example.com - they still ask for content from sub.example.com. May 16, 2020 at 1:11
  • Hope my explanation helped clarify things. Consider editing your answer to remove the mistaken info. May 16, 2020 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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