从php读取重写(mod_rewrite)文件

时间:2021-10-24 11:18:37

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.

假设您只有通过mod_rewrite规则重写的文件的URL(与应用程序托管在同一服务器上)。

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?

如何在不直接访问.htaccess文件或用于构建原始URL的重写规则的情况下,如何使用PHP读取该文件的内容?

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.

我正在尝试提取所有设置了“src”属性的脚本标记,检索目标文件的内容,将所有这些标记合并为一个大的javascript文件,缩小它然后再提供它。

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.

问题是通过file_get_contents读取所有文件似乎会降低页面速度,所以我正在寻找替代方案,如果我能够以某种方式直接从文件系统读取文件而不必在后台生成其他请求,但要做到这一点,我必须找到文件的路径,有些是通过已经重写的URL访问的。

3 个解决方案

#1


You can't include it as if it were the original PHP, only get the results of the PHP's execution itself.

您不能将它包含在原始PHP中,只能获取PHP执行本身的结果。

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.

如果你有fopen包装器,这就像在重写的URL上使用require,include或file_get_contents一样简单。否则,您可以使用fsockopen和curl作为选项来为结果创建HTTP请求。

#2


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.

由于您无法说明如何处理请求,唯一可行的解​​决方案是向该服务器发送HTTP请求。但这只会让你获得该文件/脚本的输出。

#3


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.

PHP使用apache并在文件系统级别上使用fopen-like或include-like等函数进行文件访问。重写模块不适用于此访问,因为这些函数使用OS文件访问例程但不使用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.

没有办法做到这一点,但是在php脚本中实现与.htaccess中相同的URL重写规则,因为apache-rewriting和php文件访问对彼此一无所知,并且完全不同的Web应用程序层。

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

编辑后:唯一的方法 - 通过php(不是重写模块)解析URL后,在php脚本中重写你的重写规则并使用文件系统php访问。

#1


You can't include it as if it were the original PHP, only get the results of the PHP's execution itself.

您不能将它包含在原始PHP中,只能获取PHP执行本身的结果。

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.

如果你有fopen包装器,这就像在重写的URL上使用require,include或file_get_contents一样简单。否则,您可以使用fsockopen和curl作为选项来为结果创建HTTP请求。

#2


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.

由于您无法说明如何处理请求,唯一可行的解​​决方案是向该服务器发送HTTP请求。但这只会让你获得该文件/脚本的输出。

#3


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.

PHP使用apache并在文件系统级别上使用fopen-like或include-like等函数进行文件访问。重写模块不适用于此访问,因为这些函数使用OS文件访问例程但不使用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.

没有办法做到这一点,但是在php脚本中实现与.htaccess中相同的URL重写规则,因为apache-rewriting和php文件访问对彼此一无所知,并且完全不同的Web应用程序层。

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

编辑后:唯一的方法 - 通过php(不是重写模块)解析URL后,在php脚本中重写你的重写规则并使用文件系统php访问。