0

I am trying to build a one-page dynamic site and I've hit a roadblock. Since URLs like:

index.php?page=Tutorials&Article=0

aren't really all that pretty, I decided to use .htaccess to rewrite URLs, in this case like so:

Tutorials/Articles/0

But that means most of my includes won't work since I use paths relative to the root path of the site:

js/jquery-1.9.1.min.js

I've tried to work around these by using the following code included at the beginning of index.php:

$str = dirname(__FILE__);
$str = str_replace('\\','/',$str);//since i'm using localhost for development, the paths contain backwards slahses so I replace them
$str.='/';
define('ROOT_DIR', $str);

And including everytinhg with php:

echo ROOT_DIR.'js/jquery';

But this aproach doesn't seem to work very well, even though the paths apear to be generated ok:

<script src="C:/xampp/htdocs/Tzeny/js/jquery-1.9.1.min.js"></script>

My question is can you help me find another solution to include my files corectly or to make my solution work?

For first pages it works, so:

Tzeny/Tutorials

works but

Tzeny/Tutorials/Categories/0

doesn't include anything. I used both /Tzeny/ and /. Any more ideas please? Or should I just go back to normal URLs?

3 Answers 3

2

You need to use absolute path. Example : if your domain is www.domain.com/Tzeny/index.php?page=Tutorials&Article=0 into each file like index you need include scripts like that :

<script src="/Tzeny/js/jquery-1.9.1.min.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

0

Just use absolute urls

<script src="/js/jquery-1.9.1.min.js"></script>

If you're not using virtual hosts for your local development, you can make the root of your urls a variable - so that the same code works irrespective of where you run it

Example:

<?php //pseudo code
if (on localhost) {
    $root = '/' . dirname(__FILE__); // example
} else {
    $root = '/';
}
?>

<script src="<?= $root ?>js/jquery-1.9.1.min.js"></script>

Comments

0

if the index.php is on the document root your code would work by setting

$ROOT = "/";

if it's being served out of a directory you can try a

$ROOT = "/".str_replace(rtrim($_SERVER['DOCUMENT_ROOT'],"/", '',$ROOT);

hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.