Norman
Norman

Reputation: 6365

Make a Href follow the URL in the address bar

How do you make a Href follow the URL in the address bar?

For example : If the URL in the address bar is http://www.example.com/us and my href is href='/books', on clicking the link, the link leads to http://www.example.com/books and not http://www.example.com/us/books. How do I code the href to do this? I tried several methods, but unless I code it as href='/us/books' it will not work as expected.

Upvotes: 0

Views: 630

Answers (2)

Bryan Way
Bryan Way

Reputation: 1931

There are a few different ways in which to do this, depending on the server configuration you're running your site on. The only sure way to do this in pure HTML is

<a href="/us/books/">Books</a>

Because URL links are resolved on the client side, and other configurations would complicate things (i.e. IIS Virtual applications)

If you're using ASP for a virtual application, try this:

<a href="<% Response.Write(Server.MapPath("~")); %>/Books">Books</a>

For other configurations, like PHP you could set a global constant like

define("US_ROOT_DIR", "/us");

And use it as

<a href="<?php echo US_ROOT_DIR; ?>/Books">Books</a>

But in the end, it really comes down to directory structure management since every project is different. Any way I hope this helps and if not I hope it points you in the right direction.

Upvotes: 1

Frederico
Frederico

Reputation: 91

You can use this:

<head>
    <base href="http://www.example.com/us">
</head>

then href="/books" will redirect to href="http://www.example.com/us/books"

Upvotes: 1

Related Questions