Search code examples
phphtmltemplatesradix

Best practices with multiple directories in HTML


I haven't found a clear answer to this question (but have determined the HTML tag is more trouble than it's worth.)

When you're working with multiple directories on a website, how do you make sure relative links to the rest of your site work as you change your current directory? I don't want my link to "/index.php" to actually link to "/support/index.php" when I go to the support directory.

We're using PHP, so I could use output buffering to change links, but I want to see if others have any good ideas. Could also implement it through Smarty in one way or another. I haven't built a website from scratch that has used multiple directories simply because I don't know of an easy way to deal with this, but the problem shouldn't be too difficult.

(Running on IIS, but obviously it would be better to let it work on any server.)


Solution

  • you could declare a base_url variable, or declare a constant containing your base url

    e.g.

    DEFINE('BASE_URL', 'http://example.com/');
    

    when using links

    e.g.

    <a href="<?php echo BASE_URL; ?>index.php">Home</a>