【问题标题】:Relative links on pages without trailing slash页面上没有斜杠的相对链接
【发布时间】:2015-09-15 15:43:39
【问题描述】:

在其 URL 中没有尾部斜杠的页面上,是否有任何方法可以使用将页面保留在 url 中的相对链接?

例如链接

a href="内容"

在页面上

http://www.domain.com/page/

将链接到:

http://www.domain.com/page/content

但页面上的相同链接:(注意缺少的斜线)

http://www.domain.com/page

将链接到:

http://www.domain.com/content

由于我的网站没有尾部斜杠(第二个示例),我不能使用相对链接,必须使用完整路径 (a href="/page/content")。

【问题讨论】:

  • 您已经回答了自己的问题。
  • 很抱歉,您的评论对我没有用处。也许我的问题不够清楚?
  • 我对您的问题的理解是,该链接确实会指向 /page/content 相对于 /page。尾部斜杠不必在 /page 上,链接指向 /page/content 而不是 /pagecontent 如您所料。如果这不是斜杠的意思,那么您的问题可能确实不清楚。
  • 链接将指向/content,因为页面后面没有斜线。我将编辑我的问题以使其更清楚。

标签: html


【解决方案1】:

除了首先在链接中包含斜杠或从缺少斜杠的路径重定向之外,您还可以设置基本标记,使其始终包含斜杠和当前路径。

<head>
  <base href="/page/" target="_self">
</head>

然后,只要浏览器支持,相对链接就应该按预期工作。也可以完全限定路径。您可能无法使用 javascript 动态设置它。

【讨论】:

    【解决方案2】:
    a href="./content"
    

    你可以试试这个

    【讨论】:

    • 不幸的是 ./ 在 URL 的开头没有任何影响。最后使用“/”强制访问或使用完整链接而不是相对链接似乎是这里唯一的解决方案。对于目录列表和文件的混合内容的页面来说,这很烦人。 (斜线和非斜线有效内容)。
    【解决方案3】:

    相对路径相对于 html 页面所在的文件夹起作用。

    例如,如果您在 domain.com 的 public_html 中,并且有一个名为 page 的文件夹。然后你的链接将链接到 domain.com/page/content。

    类似地,如果您在 domain.com/page 这样的路线上,您的链接被列为内容。 单击该链接会将您带到域/页面/内容。 进一步阅读: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

    <a href="linkhere.html">Click Me</a>
    This link points to a filename, with no path provided. This means that linkhere.html is located in the same folder as the page where this link appears.
    If both files were located in the root directory of the Website http://www.website.com,
     the actual website address the user would be taken to is http://www.website.com/linkhere.html. 
    If both files were located in a subfolder of the root directory called files, the user would be taken to http://www.website.com/files/linkhere.html.
    How about another example? Let's say we our http://www.website.com domain had a subfolder called pictures. Inside the pictures folder is a file called pictures.html. The full path to this page would be:
    "http://www.website.com/pictures/pictures.html"
    Still with us? Good. Let's say in this pictures.html file, we have a link:
    <a href="morepictures.html">More Pictures</a>
    If someone clicked that, where do you think it would take them? 
    If you said http://www.website.com/pictures/morepictures.html, you'd be right! 
    You probably know why it would take them there: 
    because both files are saved in the pictures subfolder.
    

    【讨论】:

    • 相对链接是根据提供它们的页面的 URL 来解析的,而不是基于服务器文件系统。
    猜你喜欢
    • 2015-03-26
    • 2016-07-09
    • 2014-10-01
    • 1970-01-01
    • 2021-01-20
    • 2019-02-10
    • 2016-07-03
    • 1970-01-01
    • 2020-08-03
    相关资源
    最近更新 更多