相对URL和尾部斜线 [英] Relative URLs and trailing slashes

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

问题描述

我以前曾经在网上浏览过这个网站,我怀疑答案是你不行,但由于我还没有找到明确的答案,所以我认为这是值得在这里问的。我发现最接近这个问题的是尾部斜线和相对网址的奥秘(目前已停用,但Google有纯文字缓存版本)。



由于传统的网页设计斜线被解释为一个目录,没有尾部斜线的那些被解释为文件资源,并且相对URL位于目录之外,那么如果当前页面具有

<$的路径p $ p> / lorem / ipsum / dolor

相对路径

  not-dolor 

将解析为

  / lorem / ipsum / not-dolor 

这自然有意义,当 / lorem / ipsum / dolor 被视为一个文件资源, dolor ,坐在一个目录中, / lorem / ipsum / ;典型,直观的约定。但是,由于大量网站现在都是动态应用程序,并且没有每个URL的文件系统映射,所以这可能会导致麻烦,因为有时您真的想要相对于路径工作,就好像在当前设计中存在斜线一样。

是否有任何合理的方式(不涉及服务器端处理/变量/其他,或JavaScript)使用基于当前路径的相对路径, em>不是当前路径的目录?因此, not-dolor 可以相对于 / lorem / ipsum / dolor 并产生

  / lorem / ipsum / dolor / not-dolor 

没有解决方法,我知道涉及类似于 ./ not-dolor 的内容,因为仍然是(/ lorem /)ipsum / 。缺少重定向到尾部的斜线并确保所有资源都具有与目录相对应的URL -ish和文件属性,或修改规范(!),有没有解决这个问题的方法?

解决方案

没有。



这个问题与导演/ (从未预料到映射将如何发生,只允许作为方便的映射,这通常很方便)。

更简单的事实是 dolor dolor / 不一样,你想从一个相对于<$ c $的引用中给出一个新的URI c> dolor / 合并为一个以 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 ),但现在它的优点似乎比简单的相对URI更方便。


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?

解决方案

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.

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.

这篇关于相对URL和尾部斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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