有链接相对于根? [英] Having links relative to root?

查看:92
本文介绍了有链接相对于根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如,在 www.example上。 com / fruits / apples / apple.html 我可以有一个链接说:

 < a href =fruits / index.html>返回水果清单< / a> 

此链接是否指向 www.example.com/fruits/apples /fruits/index.html www.example.com/fruits/index.html ?如果第一个,有没有办法让它指向第二个呢?

解决方案

根相对URL以 / 字符,看起来像< a href =/ directoryInRoot / fileName.html>链接文字< / a>

您发布的链接:< a href =fruits / index.html>返回水果清单< / a> 链接到位于名为 fruits 的目录中的html文件,该目录位于与此页面相同的目录中链接将出现。



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

 < a href =/ fruits / index.html>返回水果清单< / a> 






已编辑从评论中质疑:


这样做会使它与www.example.com相对,是否有办法指定什么是根,例如,如果我希望根在www.example.com/fruits/apples/apple.html中是www.example.com/fruits?

是,在 href src 属性中填入URL一个 / 会使路径相对于根目录。例如,给定 www.example.com/fruits/apples.html 的html页面, a of href =/ vegetables / carrots.html将会链接到页面 www.example.com/vegetables/carrots.html



base tag 元素允许您指定该页面的base-uri(尽管 base >标签必须被添加到每个页面中,在这个页面中需要使用特定的基础,因此我只会引用W3的示例:


例如,给定以下BASE声明和一个声明:



 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 // EN
http://www.w3.org/TR/html4/strict.dtd\">
< HTML>
< HEAD>
< TITLE>我们的产品< / title>
< BASE href =http://www.aviary.com/products/intro。 html>
&l吨; / HEAD>

< BODY>
< P>您见过< A href =../ cages / birds.gif>鸟笼< / 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

建议阅读:


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?

解决方案

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:

这篇关于有链接相对于根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆