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

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

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


当前回答

不只是破折号和下划线:

带空格的文本 textwithoutspaces 编码% 20空间% 20 % 20的url underscore_means_space dash-means-space + +是+空间 camelCase PascalCase “带空格的引用文本”(以及单引号和双引号) 斜杠/手段/空间 dot.means.space

其他回答

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

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

搜索引擎优化专家Jim Westergren在2005年从严格的搜索引擎优化角度对此进行了测试,得出的结论是+(+)实际上是最好的单词分隔符。然而,这似乎并不合理,可能是由于搜索引擎算法的错误。对于可读性和SEO,他推荐使用-(破折号)。

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.

对于最终用户视图,我更喜欢“about-us”或“about us”,而不是“about_us”

URL中允许有空格,所以你可以在链接中使用“/about us”(尽管这将被编码为“/about%20us”)。但老实说,这永远是个人偏好,所以这里没有真正的答案。

我会按照惯例,破折号可以出现在单词中,所以空格应该转换为下划线。