Link to index page of website

主宰稳场 提交于 2019-12-03 17:07:02

问题


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?

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?

For future viewers, I also found this question helpful.


回答1:


You can just do this:

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

Any href preceded by a slash is relative the root directory.

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.




回答2:


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

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




回答3:


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>


来源:https://stackoverflow.com/questions/10563352/link-to-index-page-of-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!