Htaccess绝对路径

时间:2015-07-22 10:59:01

标签: apache .htaccess

是否有可能获得绝对路径到我的" .htaccess"文件?

我使用它在每个文件中加载init.php

php_value auto_prepend_file /Applications/XAMPP/xamppfiles/htdocs/init.php

有没有办法让我不必写绝对路径?

像这样,如果init.php.htaccess文件属于同一类别:

php_value auto_prepend_file [ABSOLUTE_PATH_TO_HTACCESS]/init.php

如果可能,我该怎么写,但仍然返回一个目录(因为init.phppublic_html之外,.htaccess所在的位置。

提前致谢。

2 个答案:

答案 0 :(得分:1)

假设已接受的答案here的更新响应有效,并且您的相对目录结构与您描述的一样:

[docroot]
 |
 \- init.php
 |
 \- public_html
     |
     \- .htaccess
     |
     \- index.php

那么你应该可以通过包含指令

将一个目录上传到你想要添加的init.php文件中。
php_value auto_prepend_file ./../init.php

在.htaccess文件中。

答案 1 :(得分:0)

我使用PHP创建.htaccess文件解决了这个问题。

创建一个htaccess.php文件,其中包含您要放入htaccess文件中的所有内容。

php_value auto_prepend_file [INIT]init.php

然后使用正确的权限创建一个空的.htaccess文件。

然后运行下面的类并用绝对路径替换[INIT]。

以下是生成.htaccess文件的类:

<?php

    class Install {

        public static function htaccess(){

            $file = ".htaccess";

            ob_start();

                include "htaccess.php";
                $htaccess = ob_get_contents();

            ob_end_clean();

            $absolute = __DIR__;
            $init = $absolute;

            $init = str_replace("public_html", "", $init);

            $htaccess = str_replace("[INIT]", $init, $htaccess);

            $opFile = fopen($file, 'w');

            file_put_contents($file, $htaccess, FILE_APPEND);

            fclose($opFile);

        }

    }

?>
相关问题