Htaccess absolute path

只谈情不闲聊 提交于 2021-02-08 12:49:26

问题


Is it possible to get the absolut path to my ".htaccess" file?

Im using this to load init.php in every file.

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

Is there a way so I don't have to write the absolute path?

Like this, if init.php is in the same category as the .htaccess file:

php_value auto_prepend_file [ABSOLUTE_PATH_TO_HTACCESS]/init.php

and if that is possible, how do I write that but still go back one directory (Since init.php is outside public_html, where the .htaccess is).

Thanks in advance.


回答1:


Assuming the updated response on the accepted answer here works, and that your relative directory structure is as you describe:

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

then you should be able to go up one directory to the init.php file you wish to prepend by including the directive

php_value auto_prepend_file ./../init.php

in your .htaccess file.




回答2:


I solved it with using PHP to create the .htaccess file.

Create a htaccess.php file with all the things you want to put in the htaccess file.

php_value auto_prepend_file [INIT]init.php

Then create a empty .htaccess file with the correct permissions.

Then run the class below and replace [INIT] with the absolute path.

Here is the class to generate the .htaccess file:

<?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);

        }

    }

?>


来源:https://stackoverflow.com/questions/31561191/htaccess-absolute-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!