页面上的相对链接没有尾部斜杠

时间:2015-06-29 08:40:59

标签: html

在其网址中没有斜杠的网页上,有没有办法使用相关链接将页面保留在网址中?

例如,链接

a href="content"
页面上的

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")。

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

相对路径相对于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.

答案 2 :(得分:0)

a href="./content"

您可以尝试

相关问题