我们都熟悉相对路径:从 http://www.domain.com/hey
链接到 ./images/hello.jpg
的相对路径http://www.domain.com/hey/images/hello.jpg
。
问题当您位于http://www 时,如何声明到
?http://www.domain.com:1234
的相对路径.domain.com/hey
最佳答案
您不能更改相对 URL 中 authority 的任何部分(即 host:port 部分)。 请参阅 section 5.2.2 中描述的算法的 RFC 3986查看如何解释相对 URL。需要注意的重要一点是,权限只是从基本 URL 或正在解析的 URL 中复制,并且权限的结构永远不会被解释。这意味着您不能更改它的任何部分,包括端口部分。
这是从 RFC 复制的伪代码算法:
-- The URI reference is parsed into the five URI components
--
(R.scheme, R.authority, R.path, R.query, R.fragment) = parse(R);
-- A non-strict parser may ignore a scheme in the reference
-- if it is identical to the base URI's scheme.
--
if ((not strict) and (R.scheme == Base.scheme)) then
undefine(R.scheme);
endif;
if defined(R.scheme) then
T.scheme = R.scheme;
T.authority = R.authority;
T.path = remove_dot_segments(R.path);
T.query = R.query;
else
if defined(R.authority) then
T.authority = R.authority;
T.path = remove_dot_segments(R.path);
T.query = R.query;
else
if (R.path == "") then
T.path = Base.path;
if defined(R.query) then
T.query = R.query;
else
T.query = Base.query;
endif;
else
if (R.path starts-with "/") then
T.path = remove_dot_segments(R.path);
else
T.path = merge(Base.path, R.path);
T.path = remove_dot_segments(T.path);
endif;
T.query = R.query;
endif;
T.authority = Base.authority;
endif;
T.scheme = Base.scheme;
endif;
T.fragment = R.fragment;
关于http - 相对路径,但对于端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8317059/