I got a question and although I could find related information, I’m if it was exactly about what I’m wondering about.
The thing is, I got a site on http://localhost/site.
Now, when I create a link, let’s say, <a href="/posts">, it links to http://localhost/posts instead of http://localhost/site/posts.
It works fine if I remove the slash (<a href="posts">), that would be the closest and maybe the easiest solution, but I’d like to know why the links ignore the folder where the file is at?
And I also would like to know if this can be fixed with .htaccess or something.
I’ve read that a link that begins with / makes it “absolute”. So a link beginning with / is only intended to be used to link directly to the root, or to be used by sites stored at the root (in this case it wouldn’t make much sense?) ?
The leading ‘/’ at the start of the URL informs the web browser that the path given is absolute (with respect to the web server root), i.e. if you link to
/poststhen you know that the resulting link will be tohttp://www.mysite.com/posts.If you don’t supply the leading ‘/’ (and you don’t give a complete url like
http://www.mysite.com/posts) then usually the url is relative, and any page given will be relatvie to the page currently being viewed.For example:
The decision on whether to use absolute or relative links is entirely up to you – the advantage of relative links is that if your site moves, links between pages on your site will still work correctly (for example if your site moves to
www.mysite.com/otherpath, then any absolute links suchwww.mysite.com/originalpath/homewill no longer work.You should see the following site for a more complete explanation of relative urls: