105

Lets say I am currently at: http://example.com/folder/page.html

Is it possible to create a relative link on this page that points to http://example.com/folder/ without specifying folder anywhere? (And using only HTML.)

UPDATE: As it turned out ./ works only in non-strict doctype mode, while . works in both modes, so it is still a better answer in my opinion :) Thanks everybody.

Braiam
  • 1
  • 11
  • 47
  • 78
serg
  • 109,619
  • 77
  • 317
  • 330
  • 1
    Why would someone create a relative path to the current directory when by default just the name of the file itself in href assumes the current directory? – Matthew Oct 30 '17 at 05:33

7 Answers7

105

Just dot is working. The doctype makes a difference however as sometimes the ./ is fine as well.

<a href=".">Link to this folder</a>
MrChrister
  • 3,555
  • 5
  • 22
  • 34
54

For anyone who has found this thread, addressing relative paths has always created arguments over what is correct or not.

Depending on where you use the path to be addressed, it will depend on how you address the path.

Generally :

. and ./ do the same thing, however you wouldn't use . with a file name. Otherwise you will have the browser requesting .filename.ext as a file from the server. The proper method would be ./filename.ext.

../ addresses the path up one level from the current folder. If you were in the path /cheese/crackers/yummy.html, and your link code asked for ../butter/spread.html in the document yummy.html, then you would be addressing the path /cheese/butter/spread.html, as far as the server was concerned.

/ will always address the root of the site.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Mark Giblin
  • 1,086
  • 2
  • 13
  • 20
12

You can use

 ../

to mean up one level. If you have a page called page2.html in the same folder as page.html then the relative path is:

 page2.html.

If you have page2.html at the same level with folder then the path is:

  ../page2.html
Tobin Thomas
  • 106
  • 1
  • 2
  • 14
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
11
<html>
    <head>
        <title>Page</title>
    </head>
    <body>
       <a href="./">Folder directory</a> 
    </body>
</html>
Bullines
  • 5,626
  • 6
  • 53
  • 93
3

Both of the below seem to work

./

.

bdukes
  • 152,002
  • 23
  • 148
  • 175
2
<a href="./">Folder</a>
Steve T
  • 7,729
  • 6
  • 45
  • 65
2

The top answer is not clear enough. here is what worked for me: The correct format should look like this if you want to point to the actual file:

 <a href="./page.html">

This will have you point to that file within the same folder if you are on the page http://example.com/folder/index.html

Jay
  • 343
  • 5
  • 20