Absolute vs relative URLs
In general, it is considered best-practice to use relative URLs, so that your website will not be bound to the base URL of where it is currently deployed. For example, it will be able to work on localhost, as well as on your public domain, without modifications.
Absolute urls, relative urls, and...?
Here are the URL components:
http://www.example.com/en/public/img/logo.gif
\__/ \_____________/\_____________________/
#1 #2 #3
- scheme/protocol
- host
- 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.
Absolute vs relative vs slash URL?
Your first example is a URL. Your second and third examples are not URLs, they're paths. If the path begins with /
then it's an absolute path, otherwise it's a relative path.
Web browsers generally understand how to interpret a path in relation to the "current" host and path.
Javascript – absolute or relative URL in history.pushState
Reading Mdn history.pushState() reference
the last parameter, the URL, can be relative or absolute (with the same origin).
If you use relative Url is more Comfortable:
For example if your starting page is:
https://www.google.com/bar.html
and you do:
history.pushState(null,null,"foo.html");
you will get
https://www.google.com/foo.html
Note that this does not make a request to the server but it changes the url in the location address bar and in location.href
But you can use also an absolute URL according to the documentation
relative url and absolute url difference
Absolute URL is : http://stackoverflow.com/questions/3591899/relative-url-and-absolute-url-difference
and a relative URL is: /questions/3591899/relative-url-and-absolute-url-difference
Also, a relative URL can be just: ../questions/3591899/relative-url-and-absolute-url-difference
depending where is the linking page located...
Or ./3591899/relative-url-and-absolute-url-difference
if the linking page is located on the questions
folder
I will suggest to always use Relative URL... and it goes hard, keep trying to use them...
One question, why your JSPs are in the WEB-INF/
folder?
You don't have access to JSP
under the WEB-INF
folder, if you try to access it the server will throw a 404 error
. J2EE
only looks for classes and libraries under this folder.
HATEOAS: absolute or relative URLs?
There is a subtle conceptual ambiguity when people say "relative URI".
By RFC3986's definition, a generic URI contains:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
hier-part = "//" authority path-abempty
/ path-absolute
/ path-rootless
/ path-empty
foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
The tricky thing is, when scheme and authority are omitted, the "path" part itself can be either an absolute path (starts with /
) or a "rootless" relative path. Examples:
- An absolute URI or a full URI:
"http://example.com:8042/over/there?name=ferret"
- And this is a relative uri, with absolute path:
/over/there
- And this is a relative uri, with relative path:
here
or./here
or../here
or etc.
So, if the question was "whether a server should produce relative path in restful response", the answer is "No" and the detail reason is available here.
I think most people (include me) against "relative URI" are actually against "relative path".
And in practice, most server-side MVC framework can easily generate relative URI with absolute path such as /absolute/path/to/the/controller
, and the question becomes "whether the server implementation should prefix a scheme://hostname:port
in front of the absolute path". Like the OP's question. I am not quite sure about this one.
On the one hand, I still think server returning a full uri is recommended. However, the server should never hardcode the hostname:port
thing inside source code like this (otherwise I would rather fallback to relative uri with absolute path). Solution is server-side always obtaining that prefix from HTTP request's "Host" header. Not sure whether this works for every situations though.
On the other hand, it seems not very troublesome for the client to concatenate the http://example.com:8042
and the absolute path. After all, the client already know that scheme and domain name when it send the request to the server right?
All in all, I would say, recommend to use absolute URI, possibly fallback to relative URI with absolute path, never use relative path.
Absolute or Relative URL if my website may not be at the root folder?
I think you should use relative urls, and concentrate your searchs on how to use relative urls in templates, that would be resolved relatively to the final page.
I don't know the technology you are using for templating, but I see two common solutions :
- declare a "relative path" variable in the template, and then override it in the different pages, with the new relative path. Use this relative path as a prefix for all urls
- delegate urls construction to a service that would know the final page. Somethinkg like
resolveUrl(..)
Are protocol-relative URLs relative URLs?
Every relative URL is an unambiguous URL given the URL it is relative to. So if your page is http://mypage.com/some/folder/
then you know the relative URL this/that
corresponds to http://mypage.com/some/folder/this/that
and you know the relative URL //otherpage.com/
resolves to http://otherpage.com/
. Importantly, it cannot be resolved without knowing the page URL it is relative to.
A relative URL is any URL that is relative to something and cannot be resolved by itself. An aboslute URL does not require any context whatsoever to resolve.
Related Topics
Flex/Grid Layouts Not Working on Button or Fieldset Elements
How to Align 3 Divs (Left/Center/Right) Inside Another Div
Styling an Input Type="File" Button
How to Display Raw HTML Code on an HTML Page
Does Ul Have Default Margin or Padding
How to Disable Equal Height Columns in Flexbox
Proper Use of Flex Properties When Nesting Flex Containers
Html Table With 100% Width, With Vertical Scroll Inside Tbody
Two Divs Side by Side - Fluid Display
Expand a Div to Fill the Remaining Width
How to Set the Default Value For an HTML ≪Select≫ Element
Why Can't the ≪P≫ Tag Contain a ≪Div≫ Tag Inside It
How to Have an Onclick Effect in Css
One Flex/Grid Item Sets the Size Limit For Siblings
Select Option Padding Not Working in Chrome
Playing M3U8 Files With HTML Video Tag
Why Am I Seeing a 404 (Not Found) Error Failed to Load Favicon.Ico When Not Using This