htaccess 中的 auto_prepend_file 不适用于子目录

Dro*_*own 2 php apache .htaccess include

我在我的 中使用了一个auto_prepend_file指令.htaccess,如下所示:

php_value auto_prepend_file "includes/init.php"
Run Code Online (Sandbox Code Playgroud)

这是我的文件夹结构:

/php
    /css/main.css
    /includes/init.php
    /lib/functions.php
index.php
.htaccess
Run Code Online (Sandbox Code Playgroud)

这适用于根目录中的所有文件,例如index.php. 但是,它不适用于不在根目录中的所有文件,例如文件夹functions.php中的lib文件。

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

似乎它找不到应该在的文件,这是有道理的。

我试图指向我项目的根目录,但我不知道如何在.htaccess.

我尝试将我的.htaccess线路更改为以下内容:

#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"
Run Code Online (Sandbox Code Playgroud)

.htaccess无论我在层次结构中的哪个位置,我都需要放入什么才能使路径始终有效?

Nie*_*jes 5

您要么像现在一样指定一个相对路径,但随后您需要在每个文件夹中覆盖它——或者您需要从文件系统的根目录指定一个真正的绝对路径,例如:

php_value auto_prepend_file "/var/www/mywebsite/php/includes/init.php"
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它将始终工作,因为它是绝对的。

如果您在确定绝对路径时遇到问题,只需echo __DIR__在您的一个 PHP 文件中 - 它将显示它当前所在的绝对路径。