动态 URL

编程入门 行业动态 更新时间:2024-10-10 13:14:11
本文介绍了动态 URL - 有或没有尾部斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

简短的问题是 - 最佳实践是什么 - 在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 14:42:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1174841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!