Html 链接到网站的索引页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10563352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:22:38  来源:igfitidea点击:

Link to index page of website

htmlhyperlinkanchorhref

提问by Kendall Frey

Is there a way to link to the index page of a website without specifying the name of the index page or the full website URL?

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

I have this:

我有这个:

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

But when I click the link, my address bar shows:

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

mydomain.com/index.html

I would like it to show:

我希望它显示:

mydomain.com

Can I do that without putting the full URL (mydomain.com) in the href? If so, how?

我可以在不将完整 URL ( mydomain.com) 放入href? 如果是这样,如何?

For future viewers, I also found this questionhelpful.

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

回答by James Allardice

You can just do this:

你可以这样做:

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

Any hrefpreceded by a slash is relative the root directory.

任何href以斜杠开头的都是相对于根目录的。

It should be noted that when viewing a webpage from a local hard drive in a browser, this will cause the link not to function.

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

回答by Eaweb

I know this post is old and has an answer. But here is my contribution that also supports index that is not exactly in the root directory

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

to goto

要去

mydomain.com/index.html

without index.html showing in address bar

地址栏中没有显示 index.html

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

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

无论您的文件保存到哪个目录,这将始终指向 mydomain.com。

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)

这将指向 mydomain.com/subdir for mydomain.com/subdir/index.html 而接受的答案将始终指向 mydomain.com(这并不总是所需的操作)

回答by Mark Vincent Ugaddan

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

创建一个“.htaccess”文件并上传到您的主机公用文件夹

<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$ / [R=301,L]
</IfModule>