Jump to content

apache 2.4 file path variables?


Recommended Posts

I am organizing the folder structure of the site in what I thought was a sensible manner, but I am running into some major folder path headaches. There must be some sort of path variables/environment vars that can be set somewhere to alleviate this.

As an example, I put all of the common stuff here (header, footer, css, images, etc):


If a file inside of the example.com folder calls header.php, it is simple:

   <?php  include "common/header.php";  ?>

If it is two levels deeper, using relative paths (yuck - please help) it is as follows:

   <?php  include "../../common/header.php";  ?>

I won't belabor the point, but this gets messy, and I am running into "path not found" issues with nested content.

I don't want to hard-code the absolute path (all the way back to root level) for one simple reason, the development is on my desktop (Ubuntu linux w/ apache 2.4) and the paths are different from the web server.

How do I set a global path variable in apache?

Link to comment
Share on other sites

2 hours ago, justsomeguy said:

You can use .htaccess to set environment variables, and then use getenv.


thank you, that really helped (and, as usual, nothing is as easy as it sounds).

Right now I am working locally, so there is no .htaccess file, but I can set up the local variables on this desktop so my files will work on the server.

In case this helps others...

I recommend configuring separate variables for php directory links and for html source links (otherwise you will have problems).

For Ubuntu Linux, edit /etc/apache/envvars file (for Windows users I cannot help except to recommend switching to Linux, but SOME of this still pertains regardless)

1) add YOUR paths in envvars (or link to a file that contains them):

export wwwROOT=/home/user/yada.../www
export srcROOT=http://localhost:8080

2) restart apache:

 sudo service apache2 restart

3) in your php file read in these variables near the top of the file:

  $wwwROOT = getenv('wwwROOT');
  $srcROOT = getenv('srcROOT')."/example.com";


4) use the variables in php file references:

    include $wwwROOT."/example.com/yourfilepath/yourfilename.php";

5) in html references:

<a href=<?php echo $srcROOT ?> >example.com's home page...</a>

<img src=<?php echo $srcROOT."/common/myimage.png" ?> >

6) On the actual server, add the wwwROOT and srcROOT variables to the .htaccess file. The wwwROOT value will be different, but the srcROOT value should be the same*.

*I have not done step 6 yet, so if this is not correct somebody please clarify

Link to comment
Share on other sites

Alternatively, you can access the server variable

require $_SERVER['DOCUMENT_ROOT'] . "/folder_above_root/file.php";

Its no good for creating HTML URLs but with that you can just make relative from root links by starting the URL with a '/'

Edited by Funce
Link to comment
Share on other sites


Thanks, that is a great solution. I test this on the server and locally, and with that one line there is no configuration. Where were you yesterday? I was futzing around with .htaccess for hours (never got it to work locally, and it can be frustrating playing 20 questions).

I would give you a like, but I this w3schools.invisionzone site does not work very well (can't get any of the icons to function - firefox popup: "Sorry, there was a problem reacting to this content.")

  • Like 1
Link to comment
Share on other sites

Thanks, John.


I tend to only come here at the end of my workday when I've reached my wit's end with my own programs. Because of my TimeZone, sometimes it gets a little interesting, timewise. 

I wish I could've saved you the trouble, but I'm glad its solved now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...