假设您只有已通过 mod_rewrite 规则重写的文件(与应用程序托管在同一服务器上)的 URL。
如果不直接访问 .htaccess 文件或用于构建原始 URL 的重写规则,您将如何使用 PHP 读取该文件的内容?
我试图提取所有设置了“src”属性的脚本标签,检索目标文件的内容,将它们全部合并到一个大的 javascript 文件中,缩小它,然后提供该文件。
问题是通过 file_get_contents 读取所有文件似乎会减慢页面速度,因此我正在寻找替代方案,如果我能够以某种方式直接从文件系统读取文件,而不必在后台生成其他请求,但要做到这一点,我必须找出文件的路径,并且有些文件是通过已重写的 URL 访问的。
Assuming you have only the URL to a file (hosted on the same server as the app) that has been rewritten via mod_rewrite rules.
How would you read the contents of that file with PHP without having direct access to a .htaccess file or the rewrite rules used to building the original URL?
I'm trying to extract all the script tags that have the "src" attribute set, retrieve the contents of the target file, merge all of them into one big javascript file, minify it and then serve that one instead.
The problem is that reading all of the files via file_get_contents seems to slow the page down, so I was looking at alternatives and if I would be able to somehow read the files directly from the file system without having to generate other requests in the background, but to do this, I would have to find out the path to the files and some are accessed via URLs that have been rewritten.
发布评论
评论(3)
您不能像原始 PHP 一样包含它,只能包含 获取 PHP 执行本身的结果。
如果您打开了 fopen 包装器,这就像使用 require、包含 或 file_get_contents 位于重写的 URL 上。 否则你有 fsockopen 和 curl 作为选项来创建结果的 HTTP 请求。
You can't include it as if it were the original PHP, only get the results of the PHP's execution itself.
If you've got fopen wrappers on, this is as easy as using require, include or file_get_contents on the rewritten URL. Otherwise you have fsockopen and curl as options to create the HTTP request for the result.
由于您无法说明如何处理请求,因此唯一可能的解决方案是将 HTTP 请求发送到该服务器。 但这只会得到该文件/脚本的输出。
As you cannot say how the request would be handled, the only possible solution is to send a HTTP request to that server. But that would only get you the output of that file/script.
PHP 位于 apache 后面,并使用类似 fopen 或类似 include 等函数在文件系统级别上进行文件访问。 重写模块不适用于此访问,因为这些函数使用操作系统文件访问例程而不是 apache。
没有办法做到这一点,但在 php-script 中实现与 .htaccess 中相同的 URL 重写规则,因为 apache-rewriting 和 php 文件访问彼此一无所知,并且位于 Web 应用程序的完全不同的层上。
编辑后:唯一的方法 - 在 php 脚本中实现重写规则,并在通过 php 解析 URL 后使用文件系统 php 访问(不是重写模块)。
PHP lays behind apache and has file access on file-system level using fopen-like or include-like etc... functions. Rewrite module won't work for this access, because these functions use OS file access routines but not apache.
There's no way to do this, but implementing in php-script the same rules of URL-rewriting as you have in .htaccess, because apache-rewriting and php file access know nothing about each other and are on comletely different layers of web-application.
AFTER EDIT: The only way - imlement your rewrite rules in php script and use file system php access after parsing the URLs via php (not rewrite module).