【问题标题】:Htaccess absolute pathhtaccess 绝对路径
【发布时间】:2015-08-24 16:30:29
【问题描述】:

是否可以获得我的“.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.php 在public_html 之外,.htaccess 所在的位置)。

提前致谢。

【问题讨论】:

标签: apache .htaccess


【解决方案1】:

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

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

那么您应该能够通过包含该指令将一个目录转到您希望添加的 init.php 文件

php_value auto_prepend_file ./../init.php

在您的 .htaccess 文件中。

【讨论】:

  • 并非如此。相对路径相对于正在执行的 PHP 文件,而不是相关的 .htaccess 文件。所以这只有在所有 PHP 文件都在同一目录级别时才有效。如果它们不是,它可能会扩展到工作。
【解决方案2】:

我通过使用 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);

        }

    }

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 2012-01-11
    • 2010-09-15
    • 2013-04-17
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多