1

My project dir structure is

/admin
    L index.php
/includes
    L scripts.php
    L common.php
/css
/js
index.php
header.php
footer.php
...

In index.php I include /includes/scripts.php which contains references to various css files in /css dir like such:

<link rel="stylesheet" type="text/css" href="css/jquery.alerts.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />

etc...

Everything works fine when including /includes/scripts.php from the document root but if I include scripts.php from inside a different dir, ie /admin, then the paths are no longer valid and the inclusion of all files referenced fails. How can I make all paths relative to document root no matter where I call them from within my project?

bikey77
  • 6,384
  • 20
  • 60
  • 86
  • 1
    Just use local absolute paths, e.g. `/admin/index.php`. That's all based off the site's document root. – Marc B Sep 27 '13 at 21:12

2 Answers2

2

use <base href="/"/>

check this question for more information: Is it recommended to use the base html tag

or manually rework your URLS to relative to root like /css/style.css

Community
  • 1
  • 1
Adam Zielinski
  • 2,774
  • 1
  • 25
  • 36
  • 2
    w3schools is a wrong and misleading site. You shouldn't use it as reference for any sort of language. For PHP, there's the [PHP Manual](http://php.net), for JavaScript, there's [Mozilla Developer Network (or MDN)](https://developer.mozilla.org/). See http://w3fools.com to further understand why you should never use w3schools. – Madara's Ghost Sep 27 '13 at 20:58
2
<link rel="stylesheet" type="text/css" href="/css/jquery.alerts.css" />

add a slash before the url to read from web root. Keep in mind there is a difference between document root and web root. document root could resolve to something like /home/sites/www/yourdomain.com/

Adam's answer may be more precise to your needs, but in general, why are you writing your paths to not be relative to the web root if that's your goal? It seems your issue is more of a fundamental organizational error that could be resolved by employing standard practice over a work around

Alternatively, you can define a base url variable and use it to write out these links:

<?php
$base_url = 'http://www.yoursite.com/';
// $base_url = 'http://dev.yoursite.com/'; if you work locally and have defined this in your vhosts file
?>

<link rel="stylesheet" type="text/css" href="<?php echo $base_url; ?>css/jquery.alerts.css" />
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • I've already tried your suggestion and it fails. The paths to the css files are invalid. – bikey77 Sep 27 '13 at 21:04
  • The only way that would fail is if the project dir structure you outlined is not actually in the web root. Or if something else is rewriting your base, like as adam said. - what do you get when you do something like `print_r($_SERVER['DOCUMENT_ROOT'])` – Kai Qing Sep 27 '13 at 21:06
  • If you're asking if my project sits in the server root, then no, it's inside a dir of it's own by the name of the project. I'm running XAMPP and I have several project, each in their own dir inside htdocs. I hope this makes it more clear and specific. – bikey77 Sep 27 '13 at 21:13
  • right but in xampp you define the web root in the hosts file, right? So is the structure you outlined here the web root for this project? – Kai Qing Sep 27 '13 at 21:15
  • And no, I wasn't asking if the project is in document root. I just wanted to see if it was in something like www(web root)/project/css... etc. If that were the case then of course /css would fail. – Kai Qing Sep 27 '13 at 21:20
  • Yes, the structure resides inside the root of the project. I havent configured a hosts file. – bikey77 Sep 27 '13 at 21:25
  • Another alternative is to define a base url in php and call the links that way. I updated with an example. – Kai Qing Sep 28 '13 at 00:25