Have you ever wondered what is the maximum length of a domain name? It sounds like a simple enough question. However, the answer is a little less obvious that one would hope for.
Domain Name Structure
First step towards the answer is to understand what domains are made up from. Consider domains like google.com
, netflix.com
and so forth. It is important to understand that com
is already a domain - so called top-level domain (abbreviated as TLD) [1]. google
, on the other hand, is second-level domain [2]. google
and com
are also known as labels. If we take a domain name like guardian.co.uk
, guardian
, co
, and uk
would all be considered labels.
Labels have a length limit. It is 63 octets. Octet is 8 bits [3]. Thus, you can fit one ASCII character into one octet. Therefore, you can have up to 63 ASCII characters in one domain name label. However, saying that label has a maximum length of 63 characters would be a mistake. One of the reasons is that different encodings might use more than 8 bits for one character. In such a case, you’d not be able to fit 63 characters into one label.
The whole domain name can be up to 255 octets, as defined in RFC 1035, section 2.3.4 [4]. It would be reasonable to assume that you can fit up to 255 ASCII characters into a domain name. After all, there are 255 octets. However, that is not the case.
The Catch
To understand what is the actual maximum length of a domain name, we need to refer back to RFC 1035. Section 3.1 [5] states (emphasis mine):
Domain names in messages are expressed in terms of a sequence of labels. Each label is represented as a one octet length field followed by that number of octets. Since every domain name ends with the null label of the root, a domain name is terminated by a length byte of zero. The high order two bits of every length octet must be zero, and the remaining six bits of the length field limit the label to 63 octets or less.
To put it to practice, let’s take the www.directedignorance.com
as an example. It has 3 non-null domain labels (www
, directedignorance
and com
), and a null label of a root. Each of the labels is preceded with a length field indicating how many octets label consists of. To visualize this better, we could rewrite the above domain as 3www17directedignorance3com0
. Note that we have included a length byte of zero at the end, as per specification. Note also that in real life use we replace length bytes with dots for better readability, except for the first length byte and the last one, which are not usually visible.
The Answer
Even though www.directedignorance.com
is 25 characters long, the actual length of a domain name is 27. This is because we include leading and trailing length bytes in total length calculation. Taking that into account, we can see that the maximum length of a domain name is actually 253 visible characters, not 255, because we need to save 2 octets for leading and trailing length bytes.
These octets can be distributed as follows:
And if we excluded the leading and trailing length bytes:
As always, the devil is in the details!
Sources
- https://en.wikipedia.org/wiki/Top-level_domain
- https://en.wikipedia.org/wiki/Second-level_domain
- https://en.wikipedia.org/wiki/Octet_(computing)
- https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.4
- https://datatracker.ietf.org/doc/html/rfc1035#section-3.1