有相对于​​ root 的链接吗?

发布于 2024-10-31 02:17:25 字数 348 浏览 2 评论 0原文

有没有办法让页面上的所有链接都相对于根目录?

例如,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

策马西风 2024-11-07 02:17:25

根相对 URL 以 / 字符开头,类似于 链接文本

您发布的链接:返回 Fruits List 链接到名为 fruits< 的目录中的 html 文件/code>,该目录与此链接出现的 html 页面位于同一目录中。

要使其成为根相对 URL,请将其更改为:

<a href="/fruits/index.html">Back to Fruits List</a>

已编辑,以回应 OP 中评论中的问题:

这样做 / 会使其相对于 www.example.com,有没有办法指定根是什么,例如,如果我希望根是 www.example.com 中的 www.example.com/fruits 该怎么办/fruits/apples/apple.html?

是的,在 hrefsrc 属性中的 URL 前面加上 / 将使路径相对于根目录。例如,给定 html 页面 www.example.com/fruits/apples.htmlhref="/vegetables/carrots.html" 的 a 将链接到页面 www.example.com/vegetables/carrots.html

base 标签 元素允许您指定该页面的 base-uri (尽管 base 标签必须添加到每个需要使用特定基的页面,为此我将简单引用W3的例子:

例如,给定以下 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>

相对 URI“../cages/birds.gif”将解析为:

http://www.aviary.com/cages/birds.gif

引用自的示例: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 named fruits, 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:

<a href="/fruits/index.html">Back to Fruits List</a>

Edited in response to question, in comments, from 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?

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.

The base tag element allows you to specify the base-uri for that page (though the base 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:

For example, given the following BASE declaration and A declaration:

<!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:

http://www.aviary.com/cages/birds.gif

Example quoted from: http://www.w3.org/TR/html401/struct/links.html#h-12.4.

Suggested reading:

空城仅有旧梦在 2024-11-07 02:17:25

使用

<a href="/fruits/index.html">Back to Fruits List</a>

<a href="../index.html">Back to Fruits List</a>

Use

<a href="/fruits/index.html">Back to Fruits List</a>

or

<a href="../index.html">Back to Fruits List</a>
久光 2024-11-07 02:17:25

如果您要从 ASP.NET 应用程序服务器端创建 URL,并将您的网站部署到虚拟目录(例如 app2)在你的网站即
http://www.yourwebsite.com/app2/

然后插入

<base href="~/" />

标题标签后面。

因此,每当您使用相对根目录时,例如

<a href="/Accounts/Login"/> 

都会解析为“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

<base href="~/" />

just after the title tag.

so whenever you use root relative e.g.

<a href="/Accounts/Login"/> 

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.

不醒的梦 2024-11-07 02:17:25

相对路径总结(适用于href、src等):

/file_Or_FolderName          Root directory
./file_Or_FolderName         Current directory
../file_Or_FolderName        Previous directory (One level up)
../../file_Or_FolderName     Previous of previous directory (Two levels up)
../../../file_Or_FolderName  Just like above - Three levels up 

示例:

www.example.com
    ├── apple.html
    └── FolderA
        ├── fileA.html
        └── FolderB
            ├── fileB.html
            └── FolderC
                ├── fileC.html
                └── FolderD       <------ Suppose you're here (current directory)
                    ├── fileD.html
                    └── FolderE
                        └── fileE.html

下面展示如何使用相对路径访问不同级别的文件(适用于href、src 等) ETC。,)

fileD.html                 - same level access(or)
./fileD.html               - same level
./FolderE/fileE.html       - 1 level Down
../fileC.html              - 1 level Up
../../fileB.html           - 2 levels Up
../../../fileA.html        - 3 levels Up
../../../../apple.html     - 4 levels Up (or)
/apple.html                - 4 levels Up but direcly using root /

Relative Path Summary (applicable to href, src etc.,):

/file_Or_FolderName          Root directory
./file_Or_FolderName         Current directory
../file_Or_FolderName        Previous directory (One level up)
../../file_Or_FolderName     Previous of previous directory (Two levels up)
../../../file_Or_FolderName  Just like above - Three levels up 

Example:

www.example.com
    ├── apple.html
    └── FolderA
        ├── fileA.html
        └── FolderB
            ├── fileB.html
            └── FolderC
                ├── fileC.html
                └── FolderD       <------ Suppose you're here (current directory)
                    ├── fileD.html
                    └── FolderE
                        └── fileE.html

Following shows how to access the file at different levels using the relative path (applicable to href, src etc.,)

fileD.html                 - same level access(or)
./fileD.html               - same level
./FolderE/fileE.html       - 1 level Down
../fileC.html              - 1 level Up
../../fileB.html           - 2 levels Up
../../../fileA.html        - 3 levels Up
../../../../apple.html     - 4 levels Up (or)
/apple.html                - 4 levels Up but direcly using root /
岁月蹉跎了容颜 2024-11-07 02:17:25
<a href="/fruits/index.html">Back to Fruits List</a>
<a href="/fruits/index.html">Back to Fruits List</a>
葬花如无物 2024-11-07 02:17:25

要为位于根目录中的 images/ 目录的图像标签提供 URL,

`logo.png`

您应该提供以 / 开头的 src URL,如下所示

<img src="/images/logo.png"/>

:代码可以在任何目录中运行,没有任何问题,即使您在 branches/europe/about.php 中,仍然可以在那里看到徽标。

To give a URL to an image tag which locates images/ directory in the root like

`logo.png`

you should give src URL starting with / as follows:

<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.

指尖上的星空 2024-11-07 02:17:25

使用此代码“./”作为服务器上的根目录,因为它对我有用,

<a href="./fruits/index.html">Back to Fruits List</a>

但是当您在本地计算机上时,请使用以下代码“../”作为根相对路径

<a href="../fruits/index.html">Back to Fruits List</a>

Use this code "./" as root on the server as it works for me

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文