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

时间:2022-06-10 11:29:27

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.

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

<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.

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

[EDIT] In my vhost config I have this:

[编辑]在我的vhost配置中我有这个:

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

More info (phpinfo()):

更多信息(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

问候,凯文

3 个解决方案

#1


7  

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:

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

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

Place this within the vhost definition for your site.

将其放在您网站的vhost定义中。

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

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

Both of these changes will require an Apache restart.

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

EDIT:

编辑:

This is a quote from the Apache Core features page:

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

"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."

“假设访问的文件名是/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并被应用。“

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?

我认为,基于这些信息,您的php_value include_path语句正在.htaccess文件之后进行评估。如果是这种情况,则会覆盖.htaccess中用于include_path的值。你能尝试暂时从 中删除include_path行来测试这个理论吗?

#2


1  

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

你有没有在PHP代码中尝试这个(在代码的顶部):

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

#3


0  

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

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

#1


7  

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:

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

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

Place this within the vhost definition for your site.

将其放在您网站的vhost定义中。

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

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

Both of these changes will require an Apache restart.

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

EDIT:

编辑:

This is a quote from the Apache Core features page:

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

"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."

“假设访问的文件名是/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并被应用。“

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?

我认为,基于这些信息,您的php_value include_path语句正在.htaccess文件之后进行评估。如果是这种情况,则会覆盖.htaccess中用于include_path的值。你能尝试暂时从 中删除include_path行来测试这个理论吗?

#2


1  

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

你有没有在PHP代码中尝试这个(在代码的顶部):

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

#3


0  

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

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