auto_prepend_file htaccess 中不适用于子目录


auto_prepend_file in htaccess not working for subdirectories

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

php_value auto_prepend_file "includes/init.php"

这是我的文件夹结构:

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

这对于根目录中的所有文件都工作正常,例如 index.php .但是,它不适用于不在根目录中的所有文件,例如lib文件夹中的functions.php

我收到以下错误:

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

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

试图指出我项目的根源,但我不知道如何在.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"

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

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

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

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

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

相关文章: