代码之家  ›  专栏  ›  技术社区  ›  EoghanM

可移植相对URL-文件夹到域根目录

  •  2
  • EoghanM  · 技术社区  · 17 年前

    我希望编写能够正常工作的URL,无论基本URL是否为:

    http://example.com/myfolder/

    http://example.com/

    例如。

    <a href="other.html">Other</a>
    

    应该去

    http://example.com/myfolder/other.html

    http://example.com/other.html

    分别地

    但在这两种情况下,他们都会:

    http://example.com/other.html

    如果基本URL是

    http://example.com/myfolder/index.html

    http://example.com/index.html

    4 回复  |  直到 17 年前
        1
  •  3
  •   Gumbo    17 年前

    相对URI总是从基本URI解析,如果没有明确声明,则基本URI是文档的URI(请参阅 base 元素)。那么,在您的案例中,基本URI是什么?

        2
  •  2
  •   Alex Wayne    17 年前

    我怀疑您是在使用以下基本url进行测试:

    http://example.com/myfolder

    (无尾随斜杠)

    http://example.com/myfolder/

    如果基本url是这个,那么它应该像您期望的那样工作。

    通常,为了避免这些麻烦,我更喜欢绝对URL。我的webapps中几乎每个链接都采用以下样式:

    /myfolder/other.html

    它总是从任何位置指向同一个地方。

        3
  •  1
  •   Niyaz    17 年前

    如果你给的和

    <a href="other.html">Other</a>
    

    请记住,链接到文件时:

    www.example.com/folder/other.html
    

    发件人:

    www.example.com/index.html
    

    <a href="folder/other.html">Other</a>
    

    也就是说,您必须使用相对地址链接到文件。

        4
  •  0
  •   theman_on_vista theman_on_vista    17 年前

    推荐文章