Having links relative to root?

后端 未结 6 918
臣服心动
臣服心动 2020-11-22 17:02

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 li

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 17:56

    A root-relative URL starts with a / character, to look something like link text.

    The link you posted: Back to Fruits List 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:

    Back to Fruits List
    

    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:

    
    
     
       Our Products
       
     
    
     
       

    Have you seen our Bird Cages?

    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:

    • http://www.motive.co.nz/glossary/linking.php
    • http://www.communitymx.com/content/article.cfm?cid=AEDCC52C4AD230AD

提交回复
热议问题