Absolute urls, relative urls, and...?
Asked Answered
D

6

39

I am writing some documentation and I have a little vocabulary problem:

  1. http://www.example.com/en/public/img/logo.gif is called an "absolute" url, right?
  2. ../../public/img/logo.gif is called a "relative" url, right?
  3. 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?

Decern answered 24/5, 2009 at 15:58 Comment(0)
L
82

Here are the URL components:

http://www.example.com/en/public/img/logo.gif
\__/   \_____________/\_____________________/
 #1     #2             #3
  1. scheme/protocol
  2. host
  3. path

A URL is called an absolute URL if it begins with the scheme and scheme specific part (here // after http:). 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.
Lacrimatory answered 24/5, 2009 at 16:7 Comment(6)
I'm not sure if I understood that properly. Does that mean //yolo.com is considered an absolute url?Ethylene
@Ethylene No. Only if it begins with the protocol/scheme it’s considered absolute.Lacrimatory
But the browser seems to parse it properly. For example, //yolo.com would prepend the current scheme of the page. Is there any specific term, rfc, or reason why urls in browsers are parsed as absolute when prepended with //?Ethylene
@Ethylene Relative URLs are always resolved to absolute ones based on the context they are used in. Have a look at the section 5 of RFC 3986: Reference Resolution for additional information.Lacrimatory
Nice answer, but who, in everyday conversation (or user-friendly documentation), is going to always refer to it as 'a relative reference with an absolute path'? MDN is usually a reliable source, but they call it an 'absolute URL' with an 'implicit domain name', which is even worse. For as long as I can remember, these were called root-relative links. The meaning is clear (it's relative to the root), and it's so much easier to say.Hurst
The actual language used by RFC 3986 is slightly different: A relative reference that begins with two slash characters is termed a network-path reference; such references are rarely used. A relative reference that begins with a single slash character is termed an absolute-path reference. A relative reference that does not begin with a slash character is termed a relative-path reference.Scots
S
8

I have seen it called a root relative URL.

Surfactant answered 13/8, 2009 at 20:54 Comment(1)
Amazed that this answer got just one vote in over 11 years! If I were writing user-friendly documentation, that's exactly what I'd be calling it.Hurst
W
2

From the Microsoft's documentation about Absolute and Relative URLs

A URL specifies the location of a target stored on a local or networked computer. The target can be a file, directory, HTML page, image, program, and so on.

An absolute URL contains all the information necessary to locate a resource.

A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.

An absolute URL uses the following format: scheme://server/path/resource

A relative URL typically consists only of the path, and optionally, the resource, but no scheme or server. The following tables define the individual parts of the complete URL format.

  • scheme - Specifies how the resource is to be accessed.

  • server - Specifies the name of the computer where the resource is located.

  • path - Specifies the sequence of directories leading to the target. If resource is omitted, the target is the last directory in path.

  • resource - If included, resource is the target, and is typically the name of a file. It may be a simple file, containing a single binary stream of bytes, or a structured document, containing one or more storages and binary streams of bytes.

Webworm answered 26/4, 2014 at 10:26 Comment(0)
M
0

It is sometimes called a virtual url, for example in SSI:

<!--#include virtual = "/lib/functions.js" -->
Motoneuron answered 24/5, 2009 at 16:4 Comment(0)
P
0

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
Pique answered 24/5, 2009 at 16:8 Comment(0)
E
0

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 URL src="images/myimage.jpg".

The resulting url will be: https://example.com/items/images/myimage.jpg. This is because, when you don't put / before images in src="images/myimage.jpg", you are telling the browser that the images folder is located in the same directory as the menu.html file.

Imagine, inside example.com folder, has items folder, inside items folder has both images folder and menu.html file.

So it will change from https://example.com/items/menu.html to https://example.com/items/images/myimage.jpg because images folder and menu.html are on the same level when you don't put / before images. 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 is https://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 / before images to specify that it will be relative to root directory.

The resulting url will be: https://example.com/images/myimage.jpg. It will change from https://example.com/items/menu.html to https://example.com/images/myimage.jpg because images 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.

Ecstasy answered 2/4, 2023 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.