与 PHP 一起使用的 Apache Alias 或 ScriptAlias 不起作用

发布于 2024-09-18 17:35:37 字数 522 浏览 1 评论 0原文

我正在尝试在 Apache 中设置一个别名,以用于我的所有 PHP 脚本所在的 PHP 文件。通常,我们会写:

Alias /php-bin/ "/var/www/php-bin/"

不幸的是,这对 PHP 不起作用:

<?php require /php-bin/file.php; ?>

Apache 仍然可以理解别名(我做了一些测试),但是 PHP 没有按照预期的方式找到别名。

我将 PHP 用作 Apache 模块,而不是 CGI。有人有线索吗?


解决方案

使用 php.ini 中的 include_path 设置或对目录进行符号链接,以便 PHP 中的 require 函数无需使用 Apache 别名即可找到文件。在我上面的例子中,最后一个解决方案包括在我的文件系统中创建一个名为 /php-bin 的新文件夹,然后将其符号链接到 /var/www/php-bin 。

I'm trying to set an Alias in Apache to use for PHP files where all my PHP scripts reside. Usually, we write:

Alias /php-bin/ "/var/www/php-bin/"

Unfortunately this doesn't work for PHP:

<?php require /php-bin/file.php; ?>

Apache still understands the Alias (I made some tests), PHP however doesn't find the Alias the way it's supposed to.

I use PHP as an Apache module and not as a CGI. Anyone got a clue?


SOLUTION

Either use include_path setting in php.ini or symlink the directory so that the require function in PHP will find the files without using the Apache Alias. The last solution would in my case above include creating a new folder in my filesystem named /php-bin and then symlink it to /var/www/php-bin.

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

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

发布评论

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

评论(1

难道是因为include和require使用了文件系统引用,而开头的/指的是文件系统根目录。 PHP 不会引用 Apache 别名,也不会将 Web 服务器 htdocs 目录视为根目录:它将您的包含目录引用视为绝对引用(当您使用前导 / 时),或者在相对引用时使用 include_path 设置将其视为相对引用。

编辑

您可以尝试将 /var/www/php-bin/ 添加到 php.ini 中的 include_dir 中,然后只需使用 require 'file.php' (删除 /php-bin/ 目录引用)在你的代码中。

Could it be because include and require use filesystem references, and the leading / refers to the filesystem root directory. PHP doesn't refer back to the Apache alias, or treat the web server htdocs directory as root: it treats your include directory reference as an absolute (when you use a leading /) or relative using the include_path setting when it's a relative reference.

EDIT

You could try adding /var/www/php-bin/ to the include_dir in your php.ini, then simply using require 'file.php' (dropping the /php-bin/ directory reference) in your code.

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