Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto_prepend_file in htaccess not working for subdirectories

I'm using a auto_prepend_file directive in my .htaccess, like so :

php_value auto_prepend_file "includes/init.php"

This is my folder's structure :

/php
    /css/main.css
    /includes/init.php
    /lib/functions.php
index.php
.htaccess

This is working fine for all the files in the root, such as index.php. However, it will not work for all the files that are not in the root, such as functions.php that is in the lib folder.

I am getting the following error :

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

Fatal error: Unknown: Failed opening required 'includes/init.php' 
(include_path='.;C:\xampp\php\PEAR') in Unknown on line 0

It seems like it can't find the file where it should be, which makes sense.

I'm trying to point to the root of my project, but I don't know how I can do that within the .htaccess.

I tried changing my .htaccess line to the following :

#Same problem
php_value auto_prepend_file "/includes/init.php"

#Won't work for any page now
php_value auto_prepend_file "/php/includes/init.php"

What do I need to put in my .htaccess so that the path always work, no matter where I am in the hierarchy?

like image 239
Drown Avatar asked Oct 25 '25 00:10

Drown


1 Answers

Either you specify a relative path, as you do now, but then you'd need to override it in every folder - or you need to specify a real absolute path, from the root of the filesystem, like:

php_value auto_prepend_file "/var/www/mywebsite/php/includes/init.php"

In this case it will always work as it is absolute.

If you have trouble determining the absolute path, just echo __DIR__ in one of your PHP files - it will show the absolute path it is currently in.

like image 189
Niels Keurentjes Avatar answered Oct 26 '25 14:10

Niels Keurentjes