相对 URL 和尾部斜杠
我之前在网上查过这个问题,我怀疑答案是“你不能”,但由于我还没有找到明确的答案,所以我认为值得在这里问。我发现最接近这个问题的是 尾部斜杠和相对 URL 的神秘(目前已关闭,但 Google 已纯文本缓存版本)。
由于传统设计中带有尾部斜杠的 URL 被解释为目录,而那些没有尾部斜杠的 URL 则被解释为文件资源,而相对 URL 则脱离目录,那么如果当前页面的路径为
/lorem/ipsum/dolor
相对路径
not-dolor
将解析为
/lorem/ipsum/not-dolor
当 /lorem/ipsum/dolor
被视为位于目录 /lorem/ipsum 中的文件资源
;典型的、直观的约定。然而,由于现在大量网站都是动态应用程序,没有为每个 URL 映射文件系统,这可能会引起麻烦,因为有时您确实希望相对于路径进行工作,就好像在当前设计中存在尾部斜杠一样。dolor
时, 自然有意义的结果/
是否有任何合理的方法(“不涉及服务器端处理/变量/其他,或 JavaScript”)使用基于当前路径的相对路径,并且不当前路径的“目录” ?因此 not-dolor
可以与 /lorem/ipsum/dolor
相关并产生
/lorem/ipsum/dolor/not-dolor
据我所知,没有解决方法涉及 ./not-dolor 之类的内容
,因为 .
仍然是 (/lorem/)ipsum/
。如果没有重定向到尾部斜杠并确保所有资源都具有与目录和文件性质相对应的 URL,或者修改规范(!),有什么方法可以解决这个问题吗?
I've looked around the web for this before, and I suspect the answer is "you can't", but since I've not yet found an answer which is that definitive, I think it's worth asking here. The closest I've found touching on the problem is The mystery of the trailing slash and the relative url (which is currently down, but Google has a text-only cached version).
Because of the traditional design of URLs with a trailing slash being interpreted as a directory and those without a trailing slash being interpreted as a file resource, and relative URLs working off the directory, then if the current page has a path of
/lorem/ipsum/dolor
a relative path of
not-dolor
will resolve as
/lorem/ipsum/not-dolor
which naturally makes sense, when /lorem/ipsum/dolor
is viewed as a file resource, dolor
, sitting in a directory, /lorem/ipsum/
; typical, intuitive conventions. However, since a significant number of websites are now dynamic applications without a filesystem mapping for each URL, this can cause headaches, because sometimes you really want to work relative to the path as though, in the current design, there were a trailing slash.
Is there any reasonable way ("not involving server-side processing/variables/other, or JavaScript") of using a relative path based off the current path, and not the "directory" of the current path? So that not-dolor
could be relative to /lorem/ipsum/dolor
and produce
/lorem/ipsum/dolor/not-dolor
There is no workaround that I know of involving something like ./not-dolor
, since .
is still (/lorem/)ipsum/
. Short of redirecting to a trailing slash and making sure that all resources have URLs which correspond to a directory-ish and a file-ish nature, or modifying the spec(!), is there any way of tackling this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。
这个问题与导演/文件映射关系不大(从来没有预料到映射会如何发生,只允许作为一种方便的映射,这通常仍然很方便)。
这与一个简单的事实更相关,即
dolor
与dolor/
不同,并且您希望从相对于dolor/
的引用给出新的 URI code> 与以dolor
结尾的一个组合。解决方案可能是始终使用
/lorem/ipsum/dolor/
。也就是说,根本不谈论/lorem/ipsum/dolor
,只谈论/lorem/ipsum/dolor/
。毕竟,正如您所说,由于目录/文件映射不是唯一的方法,因此您的资源名称没有理由不应该始终以斜杠结尾。事实上,无论如何,这可能更有意义,因为在使用此类相对链接时,您暗示
/lorem/ipsum/dolor/not-dolor
和/lorem/ 之间存在某种关系ipsum/dolor
。现在,虽然/lorem/ipsum/dolor/not-dolor
可能与/lorem/ipsum/dolor/
没有太大关系,但暗示它可能存在在 URI 中(是的,URI 是不透明的,但虽然它们在某些级别上必须被视为不透明,但它们允许反映关系,这正是相对 URI 引用有意义的原因)。因此,可以说,/lorem/ipsum/dolor/
更清楚地反映了您的整体 URI 到资源映射(如果没有,您无论如何也不会希望从 dolor 变为非 dolor )。现在,这归结为重定向到尾部斜杠,你说你想避免这种情况(或者更好的是,从一开始就不要引导某人到
dolor
),但现在它的优点似乎不仅仅是更简单的相对 URI 的便利性。No.
The problem is not so much related to the director/file mapping (which has never been expected as how mappings would happen, only allowed as a convenient mapping, which still often are convenient).
It's more related to the simple fact that
dolor
is not the same asdolor/
and you want to give a new URI from a reference relative todolor/
when combined to one ending indolor
.What may be the solution, is to act with
/lorem/ipsum/dolor/
all the time. That is to say, never talking about/lorem/ipsum/dolor
at all, only ever about/lorem/ipsum/dolor/
. After all, since the directory/file mapping is, as you say, not the only way to do things, there's no reason why your resource names shouldn't always end in slashes.Indeed, this may make more sense anyway, as in using such relative links you are implying that there is some sort of relationship between
/lorem/ipsum/dolor/not-dolor
and/lorem/ipsum/dolor
. Now, while/lorem/ipsum/dolor/not-dolor
may not have much of a relationship to/lorem/ipsum/dolor/
, the implication that it might is there in the URI (yes URIs are opaque, but also while they must be treated as opaque at some levels, they are allowed to reflect relationships, and this is precisely why relative URI references make sense). Arguably therefore,/lorem/ipsum/dolor/
more clearly reflects your overall URI-to-resource mapping (if it didn't, you wouldn't want to be going from dolor to not-dolor anyway).Now, this comes down to redirecting to a trailing slash, which you say you want to avoid (or better yet, never leading someone to
dolor
in the first place), but its advantages now seem better than just the convenience of easier relative URIs.