Forum Moderators: open

Message Too Old, No Replies

How to deal with relative links: <base> or <?php include>?

Best way to do relative linking within HTML Pages

         

benni_203

2:00 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



Hi,

I am still trying to find the best bulletproof way to deal with relative adressing within all links of a website. It seems no option I am trying out is at all functional:

  • Option 1: Absolute adressing: http //www.example com/dir1/dir2/page.html for every link --> Problem: You can't run it on your local Apache machine or another server.

  • Option 2: <base href="www example com"> in the Header --> Problem: has to be in the header of every page. Workaround: <?php include "home.inc"> (home.inc then holds the absolute adress) in the header would be a solution, but I found that it often leads that the "home.inc" itself can't be found when the page is deep in some subdirectories.

  • Option 3: Rewriting the links and put a php statement in every link such as <a href="<?php include "home.inc";?>dir1/dir2/page.html>, but I have the same problem as in Option 2: when I am deep in a subdirectory, he has problems finding the home.inc. I put a home.inc in every subdirectory then, but that is poor.

    Does anybody has a bulletproof way to do this?

    Thanks!
    Benni

  • JAB Creations

    2:55 pm on Jul 14, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I use three different includes files to address this. For each levels (for example, level one would be ....tions.com/gallery/ versus level two ....tions.com/gallery/matrix/ and then level three for a a three deep folder file.

    I duplicate the file, then find '../' and replace with '../../'.

    If I make any large scale changes to my main includes it merely takes me a moment to fix includes2.php and includes3.php.

    I am now wary about relative links however because I've had some major f-tard requests from various bots that don't understand the concept of looking for a file in it's own folder. Files belonging to say a 1st level subfolder called "bob" will be requested from the root path. This is of course the bot's fault for not understanding relative links. It's just a total pain in my ass if I go out to slay dragons and 404s which I do every now and then. Ok just 404s... ;-)

    icpooreman

    4:05 pm on Jul 14, 2005 (gmt 0)

    10+ Year Member



    Every way has a few problems associated with it.

    You can use /dir1/dir2/page.html. The first slash represents the root dir. The problem with this is if you change where your site is located in relation to the root dir all the links will be no good.

    However if your pages aren't in your root dir but you know the path just put /pathtomydir/dir1/dir2/page.html

    If you're looking for the root in php you can use <? $_SERVER['DOCUMENT_ROOT'].'/dir1/dir2/page.html'?>

    benni_203

    7:04 pm on Jul 22, 2005 (gmt 0)

    10+ Year Member



    Thanks for all the feedback. I just changed everything to the <? $_SERVER['DOCUMENT_ROOT'].'/dir1/dir2/page.html'?> That seems to be pretty practical.

    Have a great weekend everybody,
    Benni