Strange relative links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chopficaro
Forum Commoner
Posts: 68
Joined: Fri Jan 01, 2010 12:56 am

Strange relative links

Post by chopficaro »

i have a question about how relative links work
i went to a particular website and saw that i was 2 subdirectories deep, that is i was at www.blabla.com/bla/bla/page
then i looked at the source code of the page and saw that the source of an image was src='/images/soso.png'
so one would expect that the full address of this image was
www.blabla.com/bla/bla/images/soso.png
but it wasn't!!! it was at
www.blabla.com/images/soso.png
this is not how these links usually work.
somehow, the browser knew to use the root directory, instead of the current subdirectory of the page. how did it know?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Strange relative links

Post by John Cartwright »

A relative link in html does not begin with a slash. It represents the root directory. So basically,

src='/images/soso.png' is the same as src='http://domain.com/images/soso.png'

Now.. if there was not the leading slash, which makes it relative, it would have behaved as you described.
chopficaro
Forum Commoner
Posts: 68
Joined: Fri Jan 01, 2010 12:56 am

Re: Strange relative links

Post by chopficaro »

r u kidding me? danm i feel like i should have known that

thank you
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Strange relative links

Post by John Cartwright »

.. unless of course you are using the <base> tag, which sets the root directory path.

Glad you learned something new, and your certainly welcome.
Post Reply