TazGPL
TazGPL

Reputation: 3748

Relative URIs in html anchors - recommended/preferred way

  1. <file_name>

    <a href="file.html">link</a>
    
  2. <slash><file_name>

    <a href="/file.html">link</a>
    
  3. <dot+slash><file_name>

    <a href="./file.html">link</a>
    

Which of these is recommended/preferred?
Do any of them impact the speed of retrieval?
What are the pros and cons when using either of them?

There's an extensive article on URLs by skorks here: What Every Developer Should Know About URLs, but it does not give answers to any of my questions.

Upvotes: 0

Views: 107

Answers (4)

franco_valente
franco_valente

Reputation: 88

There's no reasonable difference between them in terms of speed/retrival.
- First and third are the same case: the resource named file.html resides in the same directory of the current file.
- In the second option "/file.html" means the file is located in the webroot of your application.

Here is explained w3school

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51634

Option 1 and 3 produce the exact same HTTP GET request. Option 2 is different because a path starting with / will try to retrieve the file from the root rather than the current path.

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Number 2 is NOT the same as the others, because it resolves to the root of the website and not the current folder.

This question sounds suspiciously like micro-optimization to me, and the answer is fairly simple: The browser resolves the paths when a link is encountered, using the current URL or the <base href="..." /> if one is provided. Therefore there is absolutely no difference.

Upvotes: 1

Oded
Oded

Reputation: 499002

There is no specific recommendation.

The retrieval speed has no bearing on how the URI is constructed.

The only thing that you need to consider is whether the linking page and the page you are linking to are part of a group of pages - if you move them together then if your links are in style 2 they will break. If file.html will always be on the root of the site, then moving this file will make no difference.

I don't see the point of style 3.

Upvotes: 1

Related Questions