简体   繁体   English

相对URL和尾部斜杠

[英]Relative URLs and trailing slashes

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 ). 我发现最接近问题的是尾随斜线和相对网址的神秘感 (目前已经关闭,但Google有一个纯文本缓存版本 )。

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 由于URL的传统设计将尾部斜杠解释为目录,而没有尾部斜杠的URL被解释为文件资源,并且相对URL工作在目录之外,那么如果当前页面的路径为

/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/ ; /lorem/ipsum/dolor被视为文件资源时,自然有意义, dolor ,坐在目录中, /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. 但是,由于大量网站现在是动态应用程序而没有为每个URL映射文件系统,这可能会引起麻烦,因为有时您真的想要相对于路径工作,就像在当前设计中,有一个尾随斜杠。

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? 是否有任何合理的方法(“不涉及服务器端处理/变量/其他,或JavaScript”)使用基于当前路径的相对路径,而不是当前路径的“目录”? So that not-dolor could be relative to /lorem/ipsum/dolor and produce 所以not-dolor可能与/lorem/ipsum/dolor并且产生

/lorem/ipsum/dolor/not-dolor

There is no workaround that I know of involving something like ./not-dolor , since . 我知道没有与./not-dolor类似的解决方法,因为. is still (/lorem/)ipsum/ . 仍然是(/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(!). 没有重定向到尾部斜杠并确保所有资源都具有对应于目录和文件性质的URL,或者修改规范(!)。 Is there any way of tackling this? 有什么方法可以解决这个问题吗?

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 as dolor/ and you want to give a new URI from a reference relative to dolor/ when combined to one ending in dolor . 这与简单的事实更相关,即dolordolor/并且你希望从相对于dolor/的引用中提供一个新的URI dolor/当结合到dolor一个结尾时。

What may be the solution, is to act with /lorem/ipsum/dolor/ all the time. 可能的解决方案是与/lorem/ipsum/dolor/一起行动。 That is to say, never talking about /lorem/ipsum/dolor at all, only ever about /lorem/ipsum/dolor/ . 也就是说,从来没有谈论/lorem/ipsum/dolor ,只关于/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 . 实际上,这可能更有意义,因为在使用这样的相对链接时,你暗示/lorem/ipsum/dolor/not-dolor/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). 现在,虽然/lorem/ipsum/dolor/not-dolor可能与/lorem/ipsum/dolor/之间没有多少关系,但是它可能存在于URI中(是的URI是不透明的,但同时它们必须是在某些级别被视为不透明,它们被允许反映关系,这正是相对URI引用有意义的原因)。 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). 因此, /lorem/ipsum/dolor/更清楚地反映了你的整体URI到资源的映射(如果没有,你不会想要从dolor到not-dolor)。

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. 现在,这可以归结为重定向到结尾的斜线,你说你想避免(或更好,但从来没有导致有人dolor摆在首位),但它的优点,现在似乎比更容易相对URI的只是方便越好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM