动态 URL - 有或没有尾部斜杠? [英] Dynamic URLs - with or without a trailing slash?

查看:31
本文介绍了动态 URL - 有或没有尾部斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题是 - 最佳实践是什么 - 在 URL 中使用或不使用尾部斜杠.

The short question is - what is the best practice - to use or not to use a trailing slash in URLs.

有很多文章:this, 这个这个.

然而,它们似乎都处理静态目录结构.动态 url 怎么样,比如那些重写的,或者由中央设施处理的(比如 web 框架),根据约定/配置转发到内部资源.对他们来说,网络服务器不再实际搜索并找出确切位置的情况 - 这只是解析的问题.

However, they all seem to deal with static directory structure. What about dynamic urls, like those rewritten, or those handled by a central facility (like a web framework, for example), that forward to internal resources depending on convention/configuration. For them it is no longer the case that the web server actually searches and figures out exact locations - it is just a matter of parsing.

因此,例如使用 spring mvc,您声明在 URI 匹配 /service/action/{pathParam} 时调用给定的类方法 - 对于此 URL,imo,没有意义有一个尾部斜杠 - 请求每次都由同一个 servlet 处理,它进行解析.

So, for example with spring mvc you declare that a given class method is invoked when the URI matches /service/action/{pathParam} - for this URL, imo, it does not make sense to have a trailing slash - the request is handled by one and the same servlet each time, which does the parsing.

我认为应该保持一致 - 要么在任何地方使用尾部斜杠,要么根本不使用它们.

What I think is that one should just be consistent - either use trailing slashes everywhere, or don't use them at all.

所以:

  • 什么是最佳实践 - 有、没有或其他 - 保持一致
  • 选择一个而不是另一个的原因是什么

推荐答案

  • 最重要的是:保持一致 - 1 URL == 1 页
  • 通过 HTTP 301 重定向将一个(错误的)重定向到另一个(正确的)
  • 如果您有一个供不同人使用的网站,那么再说一遍:不要使用斜杠.
  • 在一个完美的世界里,当人们想要链接到你的东西时,他们只会复制并粘贴一个 URL.可悲的是,我们并不生活在一个完美的世界中.人们(即公关人员、营销人员)倾向于打字网址(不要问我为什么,他们就是这样做的......)然后又倾向于忘记结尾的斜杠.

    in a perfect world people would just copy&paste an URL when they want to link to your stuff. sadly we do not live in a perfect world. people (i.e. pr guys, marketing) tend to typewrite URLs (don't ask me why, they just do....) and then again tend to forget the trailing slash at the end.

    如果没有尾部斜杠,外部链接中的错误 URL 的错误率会更低.

    the wrong URLs in external links error rate is lower if you don't have trailing slashes.

    但对于大多数网站来说,这无关紧要.选择一个,坚持下去.

    but for most sites, it does not matter. choose one, stick with it.

    这篇关于动态 URL - 有或没有尾部斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆