32

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.

Community
  • 1
  • 1
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148

3 Answers3

47

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.

metamorphosis
  • 1,972
  • 16
  • 25
James Allardice
  • 164,175
  • 21
  • 332
  • 312
15

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)

Eaweb
  • 811
  • 1
  • 12
  • 16
  • When viewing a webpage from a local hard drive in a browser, this will cause the link to show the folder in most browsers. – metamorphosis Apr 10 '19 at 00:04
  • That's interesting. But I guess that is because opening a HTML file via file:/// doesn't load index.* automatically for current directory. – Eaweb Apr 10 '19 at 03:29
  • I don't know why whats wrong with mine. After viewing another part of the web, when I click home, it does not go to index.html part. I have tried: home, home, and also home. Nothing works. But I noticed, when I clicked the home button, the link add a "#" in the last button I clicked. What was that? – Agung Sudrajat Sep 08 '20 at 01:45
  • @AgungSudrajatSupriatna this post is old and your comment isn't really related to my answer. Getting # could mean a lot of things. – Eaweb Sep 09 '20 at 03:20
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>
  • What did this do...now my site is utterly horked. Guess it serves me right for pasting something I don't understand into my .htaccess file. But come on. Even after deleting the offending code it's still messed up. Every page I try to go to has the .html stripped off, and I can't access anything. – GordonM Oct 26 '18 at 06:10
  • OK, after clearing all my cache it's better. It probably didn't help that I left "yourwebsite" as-is lol. But I'm too afraid to try it again. And now I can't change my downvote. Sorry. – GordonM Oct 26 '18 at 06:19