Results 1 to 2 of 2

Thread: Strange relative links

  1. #1
    Join Date
    Dec 2009
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Strange relative links

    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?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The root directory is defined as the highest level location that some service can access.

    For links, you can either use an absolute URL (full address) or a relative path (starting from the current location).
    But there's also a third option: you can use what looks like a mix of both (and it sorta is).
    By using the initial '/' you use effectively the format of a relative path BUT by starting from the location of the root. In other words "/" can be read as "from my domain" (or subdomain).


    From every page on your site both absolute URLs and the special "/" format URLs will link to the same pages. The only difference is that the "/" format is shorter and it IS relative, but ONLY to the current domain. If the page is on google it means "http://google.com/" but if it's on yahoo it means "http://yahoo.com". Aside from that it functions exactly like an absolute URL.


    I'm not sure why you posted this in the PHP section, but since you did:

    For PHP, you can also use relative paths and absolute URLs* for functions like include() or file_get_contents(). (*For external absolute URLs, certain settings must be enabled or it won't work.)
    And you can also use the '/' method. But for PHP the root will be very different from the http '/' root location.
    PHP deals with the internal filestructure of your webserver (something completely hidden from visitors to your http site). On the site, it's just the main folder that contains the content of your domain (like 'example.com' or 'http' or 'httpdocs'). But for PHP, all of the surrounding and higher level folders are also available.
    For example, cgi-bin is a folder probably in the same level as your http folder.

    Depending on what permission PHP has on your server, the root may vary. It's possible to set it as only within that http folder, but in most cases the full path will look something like this:
    /users/username/.../httpdocs/index.php
    (for example.com/index.php)

    That higher level info might be helpful if you want to store stuff in a secure location to be retrieved by PHP or for other reasons.



    And in general the / prefix works on all paths, even system paths in other programming languages.



    Relative paths (which is essentially what this is) take several prefixes:
    ../ -- this goes up one level
    ./ -- this refers to the current level [rarely needed, usually implied]
    / -- this jumps all the way up to the root

    Note that / must be at the beginning but the other two can generally be used even in the middle of a path (though it's an odd inefficient way to write it):
    /folder1/../folder2/./index.php
    Means example.com/folder2/index.php

    (I'm not sure if that last example is entirely valid, but I've tested that a couple times out of curiosity and in general it seems to work.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •