如何通过.htaccess设置PHP包含路径?

发布于 2024-11-17 22:12:08 字数 1098 浏览 4 评论 0原文

我想将 PHP 包含路径设置为另一个目录。我尝试将其放入 .htaccess 文件中,但这不起作用。我看到的唯一一件事是我在 httpd.conf 中设置的包含路径,但不是我尝试添加的路径。

<IfModule mod_php5.c>
php_value include_path ".:/var/www/mywebsite/"
</IfModule>

我做错了什么? 我做了a2enmod mod_php5,但它已经在运行,所以我认为 PHP5 已经作为 Apache 模块运行。

[编辑] 在我的虚拟主机配置中,我有以下内容:

<Directory ~ "^/var/www/.*">
        AllowOverride All
        Options FollowSymLinks -Indexes -MultiViews
        php_value include_path ".:./include:./.include/:/var/www/.include/:/var/www/"
</Directory>

更多信息(phpinfo()):

Loaded Modules  core mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic mod_auth_mysql mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_php5 mod_reqtimeout mod_rewrite mod_setenvif mod_status

include_path    .:./include:./.include/:/var/www/.include/:/var/www/    .:/usr/share/php:/usr/share/pear

问候, 凯文

I want to set the PHP include path to another directory. I tried putting this in an .htaccess file, but that didn't work. The only thing I see is the include path's I set in httpd.conf, but not the one I tried to add.

<IfModule mod_php5.c>
php_value include_path ".:/var/www/mywebsite/"
</IfModule>

What am I doing wrong?
I did a2enmod mod_php5, but it was already running, so I suppose PHP5 is already running as Apache module.

[EDIT] In my vhost config I have this:

<Directory ~ "^/var/www/.*">
        AllowOverride All
        Options FollowSymLinks -Indexes -MultiViews
        php_value include_path ".:./include:./.include/:/var/www/.include/:/var/www/"
</Directory>

More info (phpinfo()):

Loaded Modules  core mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic mod_auth_mysql mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_php5 mod_reqtimeout mod_rewrite mod_setenvif mod_status

include_path    .:./include:./.include/:/var/www/.include/:/var/www/    .:/usr/share/php:/usr/share/pear

Regards,
Kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怂人 2024-11-24 22:12:08

Apache 必须配置为允许通过 .htaccess 覆盖设置。检查您的 Apache 配置文件 (httpd.conf) 是否覆盖您的目录:

<Directory "/directory/containing/your/htaccess/file">
    AllowOverride All
</Directory>

将其放置在您站点的虚拟主机定义中。

如果这是全局更改,您应该更改 php.ini 文件中的 include_path 设置,而不是使用 .htaccess。

这两项更改都需要重新启动 Apache。

编辑

这是来自 Apache Core 的引用功能页面

“假设正在访问的文件名为/home/abc/public_html/abc/index.html。
服务器按顺序考虑 /、/home、/home/abc、/home/abc/public_html 和 /home/abc/public_html/abc。在 Apache 1.2 中,当考虑 /home/abc 时,将匹配并应用正则表达式。在 Apache 1.3 中,树中的该点根本不考虑正则表达式。在应用所有正常的 s 和 .htaccess 文件之后才会考虑它。然后正则表达式将在 /home/abc/public_html/abc 上匹配并应用。”

我认为,根据此信息,您的 php_value include_path 语句正在之后进行评估> .htaccess 文件如果是这种情况,您在 .htaccess 中使用的 include_path 值将被覆盖,您可以尝试从其中删除 include_path 行吗? 暂时来测试这个理论?

Apache must be configured to allow settings to be overridden via .htaccess. Check your Apache config file (httpd.conf) for an override for your directory:

<Directory "/directory/containing/your/htaccess/file">
    AllowOverride All
</Directory>

Place this within the vhost definition for your site.

If this is a global change, you should change the include_path setting in your php.ini file instead of using .htaccess.

Both of these changes will require an Apache restart.

EDIT:

This is a quote from the Apache Core features page:

"Suppose that the filename being accessed is /home/abc/public_html/abc/index.html.
The server considers each of /, /home, /home/abc, /home/abc/public_html, and /home/abc/public_html/abc in that order. In Apache 1.2, when /home/abc is considered, the regular expression will match and be applied. In Apache 1.3 the regular expression isn't considered at all at that point in the tree. It won't be considered until after all normal s and .htaccess files have been applied. Then the regular expression will match on /home/abc/public_html/abc and be applied."

I think, based on this information, that your php_value include_path statement is being evaluated after the .htaccess file. If that is the case, the value you use for include_path in the .htaccess is being overwritten. Can you try removing the include_path line from <Directory> temporarily to test this theory?

想念有你 2024-11-24 22:12:08

您是否在 PHP 代码中尝试过此操作(在代码顶部):

<?php
$path = "../folder/folder/file_name.inc.php";
require_once($path);
?>

have you tried this in your PHP code (on the top of your code):

<?php
$path = "../folder/folder/file_name.inc.php";
require_once($path);
?>
总攻大人 2024-11-24 22:12:08

您可以尝试在 php 文件中使用 chdir() 来使用另一个位置作为所有文件函数的基本位置,包括 include =)

You can try to use chdir() in your php file to use another location as base location for all file functions, including includes =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文