有相对于 root 的链接吗?
有没有办法让页面上的所有链接都相对于根目录?
例如,在 www.example.com/fruits/apples/apple.html
上,我可以有一个链接说:
<a href="fruits/index.html">Back to Fruits List</a>
此链接是否指向 www.example.com/fruits/apples/ Fruits/index.html 或
www.example.com/fruits/index.html
?如果是第一个,有没有办法让它指向第二个?
Is there a way to have all links on a page be relative to the root directory?
For example, on www.example.com/fruits/apples/apple.html
I could have a link saying:
<a href="fruits/index.html">Back to Fruits List</a>
Would this link be pointing to www.example.com/fruits/apples/fruits/index.html
or www.example.com/fruits/index.html
? If the first, is there a way to have it point to the 2nd instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根相对 URL 以
/
字符开头,类似于链接文本
。您发布的链接:
返回 Fruits List
链接到名为fruits< 的目录中的 html 文件/code>,该目录与此链接出现的 html 页面位于同一目录中。
要使其成为根相对 URL,请将其更改为:
已编辑,以回应 OP 中评论中的问题:
是的,在
href
或src
属性中的 URL 前面加上/
将使路径相对于根目录。例如,给定 html 页面www.example.com/fruits/apples.html
,href="/vegetables/carrots.html" 的
将链接到页面a
www.example.com/vegetables/carrots.html
。base
标签 元素允许您指定该页面的 base-uri (尽管base
标签必须添加到每个需要使用特定基的页面,为此我将简单引用W3的例子:引用自的示例:http://www.w3.org/TR/html401/struct/links.html#h-12.4。
建议阅读:
A root-relative URL starts with a
/
character, to look something like<a href="/directoryInRoot/fileName.html">link text</a>
.The link you posted:
<a href="fruits/index.html">Back to Fruits List</a>
is linking to an html file located in a directory namedfruits
, the directory being in the same directory as the html page in which this link appears.To make it a root-relative URL, change it to:
Edited in response to question, in comments, from OP:
Yes, prefacing the URL, in the
href
orsrc
attributes, with a/
will make the path relative to the root directory. For example, given the html page atwww.example.com/fruits/apples.html
, thea
ofhref="/vegetables/carrots.html"
will link to the pagewww.example.com/vegetables/carrots.html
.The
base
tag element allows you to specify the base-uri for that page (though thebase
tag would have to be added to every page in which it was necessary for to use a specific base, for this I'll simply cite the W3's example:Example quoted from: http://www.w3.org/TR/html401/struct/links.html#h-12.4.
Suggested reading:
使用
或
Use
or
如果您要从 ASP.NET 应用程序的服务器端创建 URL,并将您的网站部署到虚拟目录(例如 app2)在你的网站即
http://www.yourwebsite.com/app2/
然后插入
标题标签后面。
因此,每当您使用相对根目录时,例如
都会解析为“http://www.yourwebsite.com/app2/帐户/登录”
这样您就可以始终相对绝对地指向您的文件;)
对我来说,这是最灵活的解决方案。
If you are creating the URL from the server side of an ASP.NET application, and deploying your website to a virtual directory (e.g. app2) in your website i.e.
http://www.yourwebsite.com/app2/
then just insert
just after the title tag.
so whenever you use root relative e.g.
would resolve to "http://www.yourwebsite.com/app2/Accounts/Login"
This way you can always point to your files relatively-absolutely ;)
To me this is the most flexible solution.
相对路径总结(适用于href、src等):
示例:
下面展示如何使用相对路径访问不同级别的文件(适用于href、src 等) ETC。,)
Relative Path Summary (applicable to href, src etc.,):
Example:
Following shows how to access the file at different levels using the relative path (applicable to href, src etc.,)
要为位于根目录中的
images/
目录的图像标签提供 URL,您应该提供以
/
开头的src
URL,如下所示:代码可以在任何目录中运行,没有任何问题,即使您在
branches/europe/about.php
中,仍然可以在那里看到徽标。To give a URL to an image tag which locates
images/
directory in the root likeyou should give
src
URL starting with/
as follows:This code works in any directories without any troubles even if you are in
branches/europe/about.php
still the logo can be seen right there.使用此代码“./”作为服务器上的根目录,因为它对我有用,
但是当您在本地计算机上时,请使用以下代码“../”作为根相对路径
Use this code "./" as root on the server as it works for me
but when you are on a local machine use the following code "../" as the root relative path