Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

url

base

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/.

like image 631
Warface Avatar asked Aug 17 '11 12:08

Warface


People also ask

How do you link a base URL?

To add a base URL, use the <base> tag in HTML. For example, you can set the base URL once at the top of your page in the header section, then all subsequent relative links will use that URL as a starting point.

Does HTML allow nested links?

Links and anchors defined by the A element must not be nested; an A element must not contain any other A elements. Since the DTD defines the LINK element to be empty, LINK elements may not be nested either.

How do you link a homepage in HTML?

To make page links in an HTML page, use the <a> and </a> tags, which are the tags used to define the links. The <a> tag indicates where the link starts and the </a> tag indicates where it ends. Whatever text gets added inside these tags, will work as a link. Add the URL for the link in the <a href=” ”>.

When linking to the same page which attribute must the anchor contain?

The href attribute makes this anchor the source anchor of exactly one link.


3 Answers

test case

It appears that if you are going to start your url (a url that depends on a base url that is) with a /, you need to specify that this is from the current directory level. Thus your link should be

<a href="./">Home</a>
like image 30
Joseph Marikle Avatar answered Oct 13 '22 01:10

Joseph Marikle


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)
like image 102
Stefan Steiger Avatar answered Oct 12 '22 23:10

Stefan Steiger


ADLADS (A Day Late - A Dollar Short)
I got here and can't relate to the dialog.
For me, I find that I wanted

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

Maybe someone else wants this?

like image 45
dcromley Avatar answered Oct 13 '22 00:10

dcromley