Walrus
Walrus

Reputation: 20464

PHP include with varying URI structure

I am using a .htaccess file to rewrite all URI addresses to take the user to index.php where some code determines which file to include based on the URL.

If the URL however is in a subfolder from that of the header the include function is not fetching the file.

eg.

index.php includes a file located at examplefolder/content/page.php. That file then includes a header on the content page but include('../header.php'); is not working. The URL is example.com/news/latest.

How can I get this to work?

Upvotes: 1

Views: 224

Answers (2)

Jeff Lambert
Jeff Lambert

Reputation: 24661

You can use the chdir() function to change the current working directory on the webserver to get your relative paths to work correctly.

To see what the current working directory is, just echo getcwd(); Once you do that, you may realize that you don't need your relative paths and you can just structure the include based off of whatever your current working directory is (if you're rewriting to index.php, then all of your pages will start from the same base path, namely the web root).

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88707

If you rewrite all URLs to /index.php, all requests will be working from the / directory (i.e. document root).

So if you are including examplefolder/content/page.php and from there you want to include the file examplefolder/header.php, then that is exactly what you would write, because including a file does not change the working directory in the current thread of execution.

E.g.

include('examplefolder/header.php');

Upvotes: 1

Related Questions