views:

142

answers:

3

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.

A: 

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.

Gumbo
The question was "How would you read the contents of that file with PHP". This way doesn't give you the contents of source file. This gives you server-side processed return of something don't know what exactly...
Jet
There is no way without knowing the exact rules. And even with the rules you would probably need a compliant mod_rewrite emulator to cover the all features of mod_rewrite like internal redirects, contextual file system lookups etc.
Gumbo
Exatly that's what I mean. The only correct answer is to implement rewrite rules on php-side.
Jet
+1  A: 

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.

Keryn Knight
A: 

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

Jet