在url中使用连字符还是下划线是更好的惯例?
应该是/about_us还是/about-us?
从可用性的角度来看,我个人认为/about-us对最终用户来说要好得多,而谷歌和大多数其他网站(以及javascript框架)使用下划线命名模式。只是风格的问题吗?破折号有兼容性问题吗?
在url中使用连字符还是下划线是更好的惯例?
应该是/about_us还是/about-us?
从可用性的角度来看,我个人认为/about-us对最终用户来说要好得多,而谷歌和大多数其他网站(以及javascript框架)使用下划线命名模式。只是风格的问题吗?破折号有兼容性问题吗?
当前回答
来自谷歌网站管理员中心
考虑在简历中使用标点符号 url。URL http://www.example.com/green-dress.html 对我们更有用吗 http://www.example.com/greendress.html。 我们建议使用连字符(-) 而不是在你的 url。
其他回答
就我个人而言,我会避免使用about-us或about_us,而使用about。
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.
我个人会避免所有的破折号和下划线,如果是在代码中,我会选择驼峰式或帕斯卡式。
维基百科上关于camelCase的文章解释了它起源背后的一些原因。它们总计为
懒惰的程序员不喜欢 到达_键 潜在的混淆 可读性 施乐PARC的“Alto”键盘 没有下划线键。
如果用户要看到字符串,那么我不会做以上任何一件事,如果我不得不使用“关于我们”或“关于我们”,因为骆驼的情况已经蔓延到一些领域的常见用法,如产品名称。比如ThinkPad, TiVo
我过去一直使用下划线,现在我只在网站的一部分使用它们,我不希望任何人直接链接,js文件,css,…等。
从SEO的角度来看,破折号似乎是首选的处理方式,对于详细的解释,来自马嘴http://www.mattcutts.com/blog/dashes-vs-underscores/。
另一个常见的问题是,当一个带下划线的超链接被加下划线时,你看不到下划线。高级用户可以解决这个问题,但Joe Public可能不会。
在代码中仍然使用下划线而不是破折号——程序员理解它们,而大多数人不理解。
一些较老的web主机和DNS服务器实际上在解析url的下划线时存在问题,所以这可能是这些约定的一部分。