绝对 url、相对 url,以及...?
我正在编写一些文档,但遇到了一些词汇问题:
http://www.example.com/en/public/img/logo.gif
被称为“绝对”url,对吗?../../public/img/logo.gif
被称为“相对”url,对吗?- 那么如何称呼它:
/en/public/img/logo.gif
?
尽管没有协议和域部分,它是否也被视为“绝对 URL”?
或者它是否被视为相对 URL,但相对于域的根?
我用谷歌搜索了一下,有些人将其归类为绝对,而另一些人则将其归类为相对。
我应该怎么称呼它? “半绝对网址”? 还是“半亲戚”? 还有一个词吗?
I am writing some documentation and I have a little vocabulary problem:
http://www.example.com/en/public/img/logo.gif
is called an "absolute" url, right?../../public/img/logo.gif
is called a "relative" url, right?- so how do you call this:
/en/public/img/logo.gif
?
Is it also considered an "absolute url", although without the protocol and domain parts?
Or is it considered a relative url, but relative to the root of the domain?
I googled a bit and some people categorize this as absolute, and others as relative.
What should I call it? A "semi-absolute url"? Or "semi-relative"? Is there another word?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,相对 URL 有两种类型:具有相对路径的相对 URL 和具有绝对路径的相对 URL。
(1) 相对URL 相对路径=相对于当前页面的URL。 需要链接的文件或资源位于同一网站内。
例如,您正在访问
https://example.com/items/menu.html
。您想要链接到与当前页面同一目录下的
images
文件夹中的图像文件,您可以使用相对 URLsrc="images/myimage.jpg"
。生成的网址将为:
https://example.com/items/images/myimage.jpg
。 这是因为,当您不在src="images/myimage.jpg"
中的images
之前放置/
时,您就是在告诉浏览器images
文件夹与menu.html
文件位于同一目录中。想象一下,在
example.com
文件夹内,有items
文件夹,在items
文件夹内有images
文件夹和>menu.html
文件。因此它将从
https://example.com/items/menu.html
更改为https://example.com/items/images/myimage.jpg
因为 <当您不在images
之前放置/
时,code>images 文件夹和menu.html
位于同一级别。 就是这样!(2) 相对URL,绝对路径=URL,指定从网站根目录开始的完整路径。
例如,您再次访问
https://example.com/items/menu.html
。您在网站的根目录中有一个
images
文件夹。 P/s:根目录是https://example.com/
。您想要链接到根文件夹中的图像文件,您可以使用绝对路径
src="/images/myimage.jpg"
的相对 URL。 请注意,在images
之前有/
来指定它将相对于根目录。生成的网址将为:
https://example.com/images/myimage.jpg
。 它将从https://example.com/items/menu.html
更改为https://example.com/images/myimage.jpg
因为images
文件夹位于根目录中。 就是这样!(3) 长话短说,
您正在访问
https://example.com/items/menu.html
。如果您使用
src="images/myimage.jpg"
,生成的 URL 将为
https://example.com/items/images/myimage.jpg
。如果您使用
src="/images/myimage.jpg"
,生成的 URL 将为
https://example.com/images/myimage.jpg
。Yes, there are two types of relative URLs: relative URLs with a relative path and relative URLs with an absolute path.
(1) Relative URL with relative path = relative to the current page's URL. Files or resources that need to be linked are located within the same website.
Example, you are on
https://example.com/items/menu.html
.You want to link to an image file in the
images
folder in the same directory as the current page, you would use the relative URLsrc="images/myimage.jpg"
.The resulting url will be:
https://example.com/items/images/myimage.jpg
. This is because, when you don't put/
beforeimages
insrc="images/myimage.jpg"
, you are telling the browser that theimages
folder is located in the same directory as themenu.html
file.Imagine, inside
example.com
folder, hasitems
folder, insideitems
folder has bothimages
folder andmenu.html
file.So it will change from
https://example.com/items/menu.html
tohttps://example.com/items/images/myimage.jpg
becauseimages
folder andmenu.html
are on the same level when you don't put/
beforeimages
. That's it!(2) Relative URLs with absolute path = URL that specify the complete path starting from the root directory of the website.
Example, you are on
https://example.com/items/menu.html
again.You have an
images
folder at the root directory of the website. P/s: Root directory ishttps://example.com/
.You want to link to an image file in root folder, you would use the relative URL with absolute path
src="/images/myimage.jpg"
. Notice that it has/
beforeimages
to specify that it will be relative to root directory.The resulting url will be:
https://example.com/images/myimage.jpg
. It will change fromhttps://example.com/items/menu.html
tohttps://example.com/images/myimage.jpg
becauseimages
folder are on the root directory. That's it!(3) Long story short,
You are on
https://example.com/items/menu.html
.If you use
src="images/myimage.jpg"
,resulting URL will be
https://example.com/items/images/myimage.jpg
.If you use
src="/images/myimage.jpg"
,resulting URL will be
https://example.com/images/myimage.jpg
.请记住可以省略 URL 的多少段,使它们成为相对的(注意:全部都是)。 这些都是有效的 URL:
http://example.com/bar?baz
?qoo=qalue
/bar2
dat/sly
//auth.example.com
(大多数人都对这个感到惊讶!将使用 http 或 https,具体取决于当前资源)#anchor
Keep in mind just how many segments of the URL can be omited, making them relative (note: its all of them, just about). These are all valid URLs:
http://example.com/bar?baz
?qoo=qalue
/bar2
dat/sly
//auth.example.com
(most people are surprised by this one! Will use http or https, depending on the current resource)#anchor
它有时被称为虚拟 url,例如在 SSI 中:
It is sometimes called a virtual url, for example in SSI:
来自 Microsoft 的有关 绝对和相对网址
From the Microsoft's documentation about Absolute and Relative URLs
以下是 URL 组成部分:
如果 URL 以方案和方案特定部分开头(此处
//< /code> 在
http:
之后)。 其他都是相对 URL。如果URL 路径以
/
开头,则称为绝对URL 路径。 任何其他URL 路径都称为相对URL 路径。因此:
http://www.example.com/en/public/img/logo.gif
是一个绝对 URL,../../public /img/logo.gif
是一个相对 URL,带有相对 URL 路径和/en/public/img/logo.gif
是一个带有绝对 URL 路径的相对 URL。注意:URI 的当前定义 (RFC 3986) 与旧的 URL 定义不同 (RFC 1738 和 RFC 1808)。
带有 URI 术语的三个示例:
http://www.example.com/en/public/img/logo.gif
是一个URI,../../public/img/ logo.gif
是一个相对引用,只有一个相对路径,而/en/public/img/logo.gif
是一个仅带有绝对路径的相对引用。Here are the URL components:
A URL is called an absolute URL if it begins with the scheme and scheme specific part (here
//
afterhttp:
). Anything else is a relative URL.A URL path is called an absolute URL path if it begins with a
/
. Any other URL path is called a relative URL path.Thus:
http://www.example.com/en/public/img/logo.gif
is a absolute URL,../../public/img/logo.gif
is a relative URL with a relative URL path and/en/public/img/logo.gif
is a relative URL with an absolute URL path.Note: The current definition of URI (RFC 3986) is different from the old URL definition (RFC 1738 and RFC 1808).
The three examples with URI terms:
http://www.example.com/en/public/img/logo.gif
is a URI,../../public/img/logo.gif
is a relative reference with just a relative path and/en/public/img/logo.gif
is a relative reference with just an absolute path.我看到它被称为根相对 URL。
I have seen it called a root relative URL.