为什么使用 - 而不是在网址中关闭_

ham*_*let 22 url

为什么使用 - 而在网址中关闭_?包含'_'的网址似乎没有不良影响.

小智 46

主机名中不允许使用下划线.因此,some_place.com不是有效的URL,因为主机名无效.URL中允许使用下划线.因此some-place.com/which_place/是完全合法的,除了其他问题.

来自RFC 1738:

主办

   [...] Fully qualified domain names take the form as described
   in Section 3.5 of RFC 1034 [13] and Section 2.1 of RFC 1123
   [5]: a sequence of domain labels separated by ".", each domain
   label starting and ending with an alphanumerical character and
   possibly also containing "-" characters. The rightmost domain
   label will never start with a digit, though, which
   syntactically distinguishes all domain names from the IP
   addresses.
Run Code Online (Sandbox Code Playgroud)


Uni*_*key 33

当您阅读a_long_sentence_with_many_underscores时,因为您正在通过字母或单词识别来阅读它,您的眼睛会沿着线条的中间跟踪,但是当您到达下划线时,您的眼睛更有可能向下追踪并备份下一个单词.

当你阅读长篇大论时,你的眼睛会沿着同一个地平线追踪,通过视觉,你的大脑更容易尝试忽略它们.

另一个很好的理由是,当单词分隔符为破折号时,Google和其他搜索引擎会将与搜索字词匹配的网址排名更高.

  • 有趣的是,它与我相反.当读a_long_sentence时,我会把目光放在中线上,所以我有效地看到了空格而不是下划线,感觉更好. (5认同)

nav*_*een 6

一个主要原因是大多数锚标记都text-decoration:underline有效地隐藏了下划线。
而且,不懂技术的用户不会自动假设有下划线:)


Cri*_*ian 5

顺便说一句......使用下划线时,似乎有几个 Java 网络库将无法正确解释 URL:

URI uri = URI.create("http://www.google-plus.com/");
System.out.println(uri.getHost()); // prints www.google-plus.com

URI uri = URI.create("http://www.google_plus.com/");
System.out.println(uri.getHost()); // prints null
Run Code Online (Sandbox Code Playgroud)

  • 这是因为下划线在域名中不合法(请参阅上面带有 RFC 1738 参考的答案),并且您的示例在域名中包含下划线。然而,在 URL 域名部分后面的 URI“路径”中使用下划线是合法的。 (3认同)