Html 有相对于 root 的链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5559578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Having links relative to root?
提问by Click Upvote
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:
例如,在www.example.com/fruits/apples/apple.html
我可以有一个链接说:
<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?
这个链接是指向www.example.com/fruits/apples/fruits/index.html
还是www.example.com/fruits/index.html
?如果是第一个,有没有办法让它指向第二个?
回答by David says reinstate Monica
A root-relative URL starts with a /
character, to look something like <a href="/directoryInRoot/fileName.html">link text</a>
.
相对于根的 URL 以/
字符开头,类似于<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 named fruits
, the directory being in the same directory as the html page in which this link appears.
您发布的链接:<a href="fruits/index.html">Back to Fruits List</a>
链接到名为fruits
的目录中的 html 文件,该目录与显示此链接的 html 页面位于同一目录中。
To make it a root-relative URL, change it to:
要使其成为根相对 URL,请将其更改为:
<a href="/fruits/index.html">Back to Fruits List</a>
Editedin response to question, in comments, from OP:
针对来自 OP 的评论中的问题进行了编辑:
So doing / will make it relative to www.example.com, is there a way to specify what the root is, e.g what if i want the root to be www.example.com/fruits in www.example.com/fruits/apples/apple.html?
这样做 / 将使它相对于 www.example.com,有没有办法指定根是什么,例如,如果我希望根是 www.example.com/fruits 在 www.example.com/fruits/苹果/apple.html?
Yes, prefacing the URL, in the href
or src
attributes, with a /
will make the path relative to the root directory. For example, given the html page at www.example.com/fruits/apples.html
, the a
of href="/vegetables/carrots.html"
will link to the page www.example.com/vegetables/carrots.html
.
是的,在href
或src
属性中的 URL 前面加上 a/
将使路径相对于根目录。例如,给定 html 页面www.example.com/fruits/apples.html
,a
ofhref="/vegetables/carrots.html"
将链接到页面www.example.com/vegetables/carrots.html
。
The base
tagelement allows you to specify the base-uri for that page (though the base
tag would have to be added to everypage in which it was necessary for to use a specific base, for this I'll simply cite the W3's example:
该base
标签元素允许你指定基URI该页面(虽然base
标签必须被添加到每个页面中它使用特定的基础是必要的,为了这个,我会简单地举W3的例子:
For example, given the following BASE declaration and A declaration:
例如,给定以下 BASE 声明和 A 声明:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Our Products</TITLE>
<BASE href="http://www.aviary.com/products/intro.html">
</HEAD>
<BODY>
<P>Have you seen our <A href="../cages/birds.gif">Bird Cages</A>?
</BODY>
</HTML>
the relative URI "../cages/birds.gif" would resolve to:
相对 URI "../cages/birds.gif" 将解析为:
http://www.aviary.com/cages/birds.gif
Example quoted from: http://www.w3.org/TR/html401/struct/links.html#h-12.4.
示例引用自:http://www.w3.org/TR/html401/struct/links.html#h-12.4。
Suggested reading:
推荐阅读:
回答by amit_g
Use
用
<a href="/fruits/index.html">Back to Fruits List</a>
or
或者
<a href="../index.html">Back to Fruits List</a>
回答by moe
<a href="/fruits/index.html">Back to Fruits List</a>
回答by A-Majeed
If you are creating the URL from the server sideof 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/
如果要创建从该URL服务器端的的ASP.NET应用程序,并部署你的网站,一个虚拟目录在您的网站(如应用2)即 http://www.yourwebsite.com/app2/
then just insert
然后插入
<base href="~/" />
just after the title tag.
就在标题标签之后。
so whenever you use root relative e.g.
所以每当你使用 root 相对时,例如
<a href="/Accounts/Login"/>
would resolve to "http://www.yourwebsite.com/app2/Accounts/Login"
将解析为“ 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.
对我来说,这是最灵活的解决方案。
回答by Tum Ozel Okullar
To give a URL to an image tag which locates images/
directory in the root like
为图像标签提供一个 URL,该标签images/
在根目录中定位目录,如
`logo.png`
you should give src
URL starting with /
as follows:
你应该给出如下src
开头的 URL /
:
<img src="/images/logo.png"/>
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.
此代码适用于任何目录,即使您处于branches/europe/about.php
静止状态,也可以在那里看到徽标。
回答by king zecole
Use this code "./" as root on the server as it works for me
使用此代码“./”作为服务器上的 root 用户,因为它对我有用
<a href="./fruits/index.html">Back to Fruits List</a>
but when you are on a local machine use the following code "../" as the root relative path
但是当您在本地机器上时,请使用以下代码“../”作为根相对路径
<a href="../fruits/index.html">Back to Fruits List</a>