Ad

Confused About Relative Url Anchor Tag

- 1 answer

I'm confused about relative href url in anchor tag. I have a case below:

I have a homepage that can be accessed with abc.com/it or abc.com/it/index with return the same page. In that page I have anchor tag with relative url href='./about' (I can not use absolute or root-relative url in this case). The problem is:

  • When I access the page through url abc.com/it/index then click the anchor link, it will redirect to abc.com/it/about (this is my expected result).

  • But if I access the page through abc.com/it then click the link, it
    will redirect to abc.com/about (this is not my expectation).

Anyone please give me a solution for this. Thank you all.

Ad

Answer

The solution is to add a trailing slash to your base URL, i.e. abc.com/it/.

Here is the relevant line from the specification of relative URLs (RFC 1808)

The last segment of the base URL's path (anything following the rightmost slash "/", or the entire path if no slash is present) is removed and the embedded URL's path is appended in its place.

I would also suggest adding a redirect from abc.com/it to abc.com/it/

Ad
source: stackoverflow.com
Ad