最近在工作的时候发现,对于域名解析服务的命名方式不是很了解。有的时候用了驼峰方式以及下划线,但是并不可以正确解析。在网上搜索相关的文章真的少之又少,无奈之下只能自己翻看RFC标准文档了。
DNS标准中关于域名语法的定义
描述DNS所对应的RFC文档是RFC 1034和RFC 1035,我们这里就只看1034就可以了(文档居然是在1987年写的,比我大半轮呢......90后的老鲜肉表示害怕)。好在编写文档的大佬语言比较平易近人,读起来完全没有啥晦涩的地方。
我们翻到文章的第3章-DOMAIN NAME SPACE and RESOURCE RECORDS。
其中的第5节Preferred name syntax对于名称的约定做出了解释。下面截取一部分:
Note that while upper and lower case letters are allowed in domain
names, no significance is attached to the case. That is, two names with
the same spelling but different case are to be treated as if identical.
The labels must follow the rules for ARPANET host names. They must
start with a letter, end with a letter or digit, and have as interior
characters only letters, digits, and hyphen. There are also some
restrictions on the length. Labels must be 63 characters or less.
For example, the following strings identify hosts in the Internet:
A.ISI.EDU XX.LCS.MIT.EDU SRI-NIC.ARPA
翻译整理上面的话:
- 对于域名长度的要求是小于等于63位
- 必须以字母开头,以字母或数字结尾
- 其中除了数字和字母之外,只能包含连字符“-”(言外之意使用下划线不合法)
- 对于拼写的大小写不敏感,也就是说www.greatytc.com等价于www.JIANSHU.com
HTTP/1.1中关于地址的语法规范
上面是DNS关于域名的规范,由于现在服务之间主要还是通过http/1.1协议。所以落到实处的话,还要以HTTP标准文档中的描述为准。HTTP/1.1是RFC2616,其中有关URI比较的部分在第3章第2节的第3小节,下面贴出来:
When comparing two URIs to decide if they match or not, a client
SHOULD use a case-sensitive octet-by-octet comparison of the entire
URIs, with these exceptions:
- A port that is empty or not given is equivalent to the default
port for that URI-reference;
- Comparisons of host names MUST be case-insensitive;
- Comparisons of scheme names MUST be case-insensitive;
- An empty abs_path is equivalent to an abs_path of "/".
Characters other than those in the "reserved" and "unsafe" sets (see
[RFC 2396](https://tools.ietf.org/html/rfc2396) [[42](https://tools.ietf.org/html/rfc2616#ref-42 ""Uniform Resource Identifiers (URI): Generic Syntax and Semantics"")]) are equivalent to their ""%" HEX HEX" encoding.
For example, the following three URIs are equivalent:
[http://abc.com:80/~smith/home.html](http://abc.com/~smith/home.html)
[http://ABC.com/%7Esmith/home.html](http://abc.com/~smith/home.html)
[http://ABC.com:/%7esmith/home.html](http://abc.com/~smith/home.html)</pre>
上面我们可以看到那两个大写的MUST,说明host部分和scheme部分不区分大小写。
结论
以下写法
HTTP://WWW.JIANSHU.COM
Http://WWW.jianshu.COM
hTTP://www.JIANSHU.COM
HttP://www.jianshu.COM
hTTp://WwW.jIaNsHu.CoM
//www.greatytc.com
...
都是等价的。如果要在里面使用起连接作用的符号,只能使用“-”。