使用 PHP 解析 .htaccess 文件
我正在使用 Zend Framework,并使用 .htaccess
进行某些设置。我现在正在编写用于调度的命令行脚本(例如 cron)。命令行脚本不会查看 .htaccess
文件,因为 Apache 不提供它们。我想用我的脚本解析 .htaccess
以检索一些设置。以下是我特别感兴趣的行:
SetEnv APPLICATION_ENV development
php_value date.timezone America/New_York
我注意到 PEAR File_HtAccess 包,但它似乎只解决 .htaccess
文件的身份验证部分。
解决方案:(归功于Bamieater< /a>)
用于调试输出的 echo
语句,已从工作代码中删除。
$htaccess = file(realpath(dirname(__FILE__)) . '/.htaccess');
echo '<pre>';
foreach ($htaccess as $line) {
if (preg_match('/^\s*SetEnv\s+APPLICATION_ENV\s+(.*?)\s*$/', trim($line), $matches)) {
defined('APPLICATION_ENV') || define('APPLICATION_ENV', $matches[1]);
echo APPLICATION_ENV . PHP_EOL;
} elseif (preg_match('/^\s*php_(?:admin_)?value\s+date\.timezone\s+(.*?)\s*$/', $line, $matches)) {
date_default_timezone_set($matches[1]);
echo date_default_timezone_get() . PHP_EOL;
}
}
echo '</pre>';
I am using the Zend Framework, and I use the .htaccess
for some settings. I am now writing command line scripts for scheduling (e.g. cron). Command line scripts don't look at the .htaccess
file because they're not served up by Apache. I would like to parse the .htaccess
with my script to retrieve some settings. Here are the lines I'm specifically interested in:
SetEnv APPLICATION_ENV development
php_value date.timezone America/New_York
I noticed the PEAR File_HtAccess package, but it seems to only address authentication portions of the .htaccess
file.
SOLUTION: (with credit due to Bamieater)
echo
statements for debug output, removed from working code.
$htaccess = file(realpath(dirname(__FILE__)) . '/.htaccess');
echo '<pre>';
foreach ($htaccess as $line) {
if (preg_match('/^\s*SetEnv\s+APPLICATION_ENV\s+(.*?)\s*$/', trim($line), $matches)) {
defined('APPLICATION_ENV') || define('APPLICATION_ENV', $matches[1]);
echo APPLICATION_ENV . PHP_EOL;
} elseif (preg_match('/^\s*php_(?:admin_)?value\s+date\.timezone\s+(.*?)\s*$/', $line, $matches)) {
date_default_timezone_set($matches[1]);
echo date_default_timezone_get() . PHP_EOL;
}
}
echo '</pre>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每行读取 .htaccess 文件并使用正则表达式来访问数据。
例如这样的事情:
Read the .htaccess file per line and use a regular expression to access the data.
For example something like this:
借用 Bamieater 的一点,这里有一个更完整的函数,您可以将其放入 cron 作业文件的开头,用于设置环境变量并包含 php_prepend_file。
Borrowing a bit from Bamieater, here's a more complete function you can drop into the beginning of your cron job file that sets the environment variables and includes the php_prepend_file.
解析
.htaccess
尽管很容易做到,但在我看来并不是最好的解决方案。环境变量可能设置在不同的位置,因此您必须检查所有文件(例如,主 Apache 配置、其他 .htaccesses)。
我建议为 shell 脚本设置单独的环境变量,
我将这些设置保留在服务器的全局属性中(apache config 和 .bashrc),因此所有应用程序都会自动知道它们在哪里,而无需在部署时更改文件。
Parsing
.htaccess
, despite it is easy to do, is not the best solution in my opinion.The environment variable may be set in different places, so you'd have to check all the files (eg. main Apache configuration, other .htaccesses).
I'd recommend setting separate environment variable for your shell scripts,
I keep those settings in global properties of the server (apache config and .bashrc), so automatically all the apps know where they are without changing the files upon deployment.
答案很晚,但您(或进入此线程的其他人)可能会感兴趣。
我用 PHP 编写了一个小型
.htaccess
解析器,它使人们能够将文件作为 ArrayObject 进行操作。您可以在此处 (GitHub) 查看该项目。
A late answer but it might interest you (or others that come into this thread).
I wrote a little
.htaccess
parser, in PHP, that enables one to manipulate the file as an ArrayObject.You can check the project here (GitHub).