页面上的相对链接,不带斜杠

Ron*_*eva 5 html

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

例如,链接

href =“ content”

在页面上

http://www.domain.com/page/
Run Code Online (Sandbox Code Playgroud)

将链接到:

http://www.domain.com/page/content
Run Code Online (Sandbox Code Playgroud)

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

http://www.domain.com/page
Run Code Online (Sandbox Code Playgroud)

将链接到:

http://www.domain.com/content
Run Code Online (Sandbox Code Playgroud)

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

小智 6

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

<head>
  <base href="/page/" target="_self">
</head>
Run Code Online (Sandbox Code Playgroud)

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


Dav*_*d G -2

相对路径相对于 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.
Run Code Online (Sandbox Code Playgroud)

  • 相对链接是根据它们所服务的页面的 URL 来解析的,而不是根据服务器文件系统。 (3认同)