John Smith John Smith - 4 months ago 13x
HTML Question

How should I create a link to a directory within the same subdomain?

I currently have an index file saved under a url of the form
. Within directory 1, I have another directory called
. To access the index page for
I have used a link of the form
<a href="/directory2">Link</a>

I would have expected this link to lead the user to the page
when clicked, but instead it directs to

Why does this happen?


You should use the complete path:

<a href="/directory1/directory2">Link</a>

That happens because you're using a relative url.


For instance: What is relative URL?

Lets assume your script (index.html in this example) is being executed at with this folder structure:

   - img.jpg
   - banners/
       - banner1.jpg
       - banner2.jpg

If you want to create a link to 'uploads' folder, all you have to do is

<a href="uploads/">go to uploads</a>

That is because they are in the same structure level.

In the other hand, if you want to link one of the banners, you'd have to "navigate" trough images and then banners. It means that relative to your script, there is images/banner/,