I have a dynamic website that can be accessed as domain/categories or domain/categories/article, and it redirects to domain/index.php?category/article.
The problem I'm facing is that I want to use relative paths from the files. I managed to do that for includes with dirname(FILE), as in
ini_set('include_path', dirname(__FILE__) . '/include');
This works fine. Depending on the directory I will change it to, and it also works
ini_set('include_path', dirname(__FILE__) . '/../include'); (extra /../)
But if I try to do the same with img src, A href or php Header, while I get the correct file path to the images or links, they simply do not work. Nothing happens as if I didn't click at all. Clicking on a link generated through php with:
print "<a href='" . dirname(__FILE__) . strtolower(str_replace(" ", "", $row['pageSection'])) . "/" . strtolower(str_replace("", "-", $row['pageTitle'])) . "' alt='' >";
Does not work, even if the html source code is reading as:
How can I make the path to the img, link (and php header) relative to the file location, and no the url, and make it work?
Side note: even my CSS brakes if I acess my site through domain/prettyurl/, but not if I access through domain/prettyurl (no ending slash), pretty weird.
it looks like you are running your php on a localhost so
is returning a directory ie "C:\validation..."
"'http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/include'"
as your include path
Got it working on local host AND online, here is my setup so:
1) You may use it yourself 2) Give a better alternative to us newbies
I used what zoranc said above. For example, to get CSS to work on the dynamic-pretty-url pages, I set it up as:
<?php print "<link href='http://" . $_SERVER['HTTP_HOST'] .'/' . dirname($_SERVER['PHP_SELF']) . "/CSS/main.css' rel='stylesheet' type='text/css' />";
WARNING: the code above will produce links such as Domain///CSS. I still need to filter the extra slashes out, but it works even as it is for the CSS and everything else (img, a, php headers)
Then I had to do this everytime I wanted to:
- Create a dynamic link
- Create a dynamic img
Even for my header form! Since my header is always included, when you are browsing pretty-urls, your form can't have action='header.php', because it will then try to find it on whatever pretty-url you are currently browsing (domain/category/article and so on). You'll have to use header='" php the solution we are discussion/header.php endphp.
Again, be warned this produces extra slashes and you'll have to remove them somehow.
With this setup you can have a site that works on your localhost and any web host without the need to change code, but it is somewhat cumbersome and error prone to use. If anyone has a better solution please share.
This works well enough and in my opinion is definitely better than having to change code when you upload your site everytime, which is even more error prone I suppose.
Thanks again Zoranc, really helpful.