Search code examples
htmlurlradix

How to link to the home URL in a subdirectory when the base href is set to that subdirectory?


Here an example of my base URL

<base href="http://subdomain.example.com/folder/" />

I would like to know how to call that base URL for the HOME link when I try

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

It goes to http://subdomain.example.com/ and does not include /folder/.


Solution

  • What you need is the virtual directory name of your application/website.
    This is a server variable, and it is not possible to get it in HTML alone, you need some server side component, like PHP or ASP.NET.

    If you are in the root directory, you can try a relative link.
    So try using a . to indicate "from this folder"

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

    Should your question actually concern the opposite direction, do this:
    <a href="../">Home</a>
    

    Or use an absolute link:
    <a href="/folder">Home</a>
    

    Or use a canonical link

    <a href="http://subdomain.domain.com/folder">Home</a>
    

    By the way, you could also set a protocol-relative-link:

    <a href="//subdomain.domain.com/folder">Home</a>
    

    Or if you know where exactly you are right at the moment of the http-request relative to the root, you can infer the root directory link in JavaScript: e.g. assuming you have "index.htm" in the root-directory, then from a javascript in index.htm, you can get the virtual directory name like this:

    document.location.pathname.substr(0, document.location.pathname.length-"index.htm".length)