问题
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