I'm trying to get the right path and so far I have this which kind of works, but not if the file is in a sub directory.
These links I have in my navigation:

    <li><a href="<?php echo $_SERVER['DOCUMENT_URI'].'about/organisation.php'; ?>">Organisation</a></li>
    <li><a href="<?php echo $_SERVER['DOCUMENT_URI'].'about/team.php'; ?>">Team</a></li>

This works from my index.php , but not from organisation.php and team.php in the 'about' directory. If I want to open 'team.php from within organisation.php, I get an URL in the address bar like this:


As you can see I get 'about' twice, so what is the right way to tackle this issue?

Worth to mention that I use a PHP include to add the navigation on every page, so I can't change the links, becaue I could solve it by excluding about/ if I didn't use a PHP include for the navigation.
<a href="<?php echo $_SERVER['DOCUMENT_URI'].'team.php'; ?>">Team</a>

Edited by gentlemedia

1 Year
Discussion Span
Last Post by gentlemedia

$_SERVER['DOCUMENT_URI']. will echo the directory you are in so it will always cause a problem. is this not ok or is it interfering with your .htaccess somehow?

<ul> <li><a href="<?php echo 'index.php'; ?>">Organisation</a></li> <li><a href="<?php echo 'about/organisation.php'; ?>">Organisation</a></li> <li><a href="<?php echo 'about/team.php'; ?>">Team</a></li> </ul>


If you can't use relative references - if you're including a file in different pages at different depths, then you may need to go absolute.

If you have a config file set up somewhere, you can set constants for locations:

define('PUBLIC', '/projects/ecwc/concept/' ); 


    <li><a href="<?php echo PUBLIC .'about/organisation.php'; ?>">Organisation</a></li>
    <li><a href="<?php echo PUBLIC .'about/team.php'; ?>">Team</a></li>

Quite a few ways to do this though.


Hi Diafol, I don't have a config file setup... should I?
I've tried your method by including it at the top of every page like:
<?php define('PUBLIC', '/projects/ecwc/concept/' ); ?>
But now it stops rendering the HTML where the navigation shoud appear, so is there an error in <?php echo PUBLIC .'about/organisation.php'; ?> somehow?


Shouldn't have thought so. echo out the url to see wthe screen to that you see what you get. Do not redefine the constant though. This is why we have a config.php file, so that defaults can be set and re-used everywhere. If you include a file that has the definition inside another file which also has the definition, you may get issues.


Ok, I got it fixed!
I've used a config.php which sits in the 'projects' directory and has <?php define('PUBLIC_URI', '/projects/ecwc/concept/' ); ?> in it.
I then include it on every page with relative paths like so <?php include '../../config.php'; ?> (example from index.php).
The links in the navigation are like this: <a href="<?php echo PUBLIC_URI.'about/organisation'; ?>">Organisation</a>

@diafol - The error before was because I needed to write 'PUBLIC_WORD' (I've chosen URI for that word) . It needs to be for whatever reason a word-underscore-word if I'm right.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.