在url中使用连字符还是下划线是更好的惯例?

应该是/about_us还是/about-us?

从可用性的角度来看,我个人认为/about-us对最终用户来说要好得多,而谷歌和大多数其他网站(以及javascript框架)使用下划线命名模式。只是风格的问题吗?破折号有兼容性问题吗?


当前回答

以下是使用破折号的几点建议:

谷歌建议在下划线上使用破折号。 终端用户更熟悉破折号。 破折号更容易在标准键盘上书写(不需要Shift键)。 破折号不会隐藏在下划线后面。 破折号在url的上下文中感觉更原生,因为它们在域名中是被允许的。

其他回答

来自谷歌网站管理员中心

考虑在简历中使用标点符号 url。URL http://www.example.com/green-dress.html 对我们更有用吗 http://www.example.com/greendress.html。 我们建议使用连字符(-) 而不是在你的 url。

I'm more comfortable with underscores. First of all, they match in with my regular programming experience of variable_names_are_not-subtraction, second of all, and I believe this was mentioned already, words can have hyphens, but they do not ever have underscores. To pick a really stupid example, "Nation-state country" is different from "nation state country". The former translates something like "the land of nation-states" (think "this here is gun country! Best move along, y'hear?"), whereas the latter looks like a list of sometime-synonyms. http://example.com/nation-state-country/ doesn't appear to mean the same as http://example.com/nation-state_country/, and yet, if hyphens are delimiters/"space"s in addition to characters in words, it can. The latter seems more clear as to the actual purpose, whereas the former looks more like that list, if anything.

Jeff对此有一些想法:https://blog.codinghorror.com/of-spaces-underscores-and-dashes/

两者都有缺点。我建议你选择一个并保持一致。

就我个人而言,我会避免使用about-us或about_us,而使用about。

这只是一个猜测,但似乎他们选择了一个人们最不可能在名字中使用的名字。通过这种方式,您可以拥有一个包含连字符的单词的名称,并且仍然使用下划线作为单词分隔符,例如UseTwo-wayLinks可以转换为use_two-way_links。

在您的示例中,/about-us将是一个名为“about-us”的连字符目录(如果存在这样的单词,/about_us将是一个名为“about us”的两个单词短语的目录,转换为一个非白人字符字符串。