29

有没有办法在不指定索引页面名称或完整网站 URL 的情况下链接到网站的索引页面?

我有这个:

<a href="index.htm">Home</a>

但是当我点击链接时,我的地址栏显示:

mydomain.com/index.html

我希望它显示:

mydomain.com

我可以在不将完整 URL ( mydomain.com)放入的情况下执行此操作href吗?如果是这样,怎么做?

对于未来的观众,我也发现这个问题很有帮助。

4

3 回答 3

39

你可以这样做:

<a href="/">Home</a>

任何href前面有斜线的都是相对的根目录。

需要注意的是,在浏览器中从本地硬盘查看网页时,会导致链接失效。

于 2012-05-12T11:37:42.040 回答
14

我知道这篇文章很旧并且有答案。但这是我的贡献,它也支持不完全在根目录中的索引

要去

mydomain.com/index.html

without index.html showing in address bar

<a href="/">Home</a>

this will always point to mydomain.com no matter where the directory your file was saved to.

to point to the index in a directory use this:

<a href="./">Home</a>

this will point to mydomain.com/subdir for mydomain.com/subdir/index.html while the accepted answer will always point to mydomain.com (and this is not always the desired action)

于 2016-12-06T15:01:16.670 回答
1

Create a ".htaccess" file and upload to your host public folder

<IfModule mod_rewrite.c> 
RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
RewriteRule ^index\.html$ http://www.yourwebsite.com.ph/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html 
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
</IfModule>
于 2018-09-03T04:06:32.747 回答