views:

258

answers:

3

I am working on a website that passes all PHP scripts through a single PHP page (passthrough.php).

The .htaccess file is supposed to take certain special directories and translate them into GET variables, so the controller script knows what to do with them. So:

http://example.com/ajax/method.php

becomes

http://example.com/passthrough.php?path=method.php&mode=ajax

and

http://example.com/ajax/json/method.php

becomes

http://example.com/passthrough.php?path=method.php&mode=ajax&output=json

And so on. This is what I have in my .htaccess file:

RewriteRule ajax/(.*)$ $1?mode=ajax [QSA]
RewriteRule (xml|json)/(.*)$ $2?output=$1 [QSA]

RewriteRule (.*)\.php$ passthrough.php?path=$1 [QSA]
RewriteRule passthrough.php - [L]

In my second example, mod_rewrite executes the ajax rule, then it looks like it executes the passthrough.php rule. Then, it looks to run the rewrite process. Now the ajax rule does not fit, but the output rule does, so that is applied, and then the URL is finally rewritten again by the passthrough.php rule, which results in the following GET vars:

path: method.php/json/method.php/json/method output: json mode: ajax

My question is this: is there a way to get the rewrite engine to process the whole URL in one go? so the url comes in as /ajax/json/method.php, becomes /json/method.php?mode=ajax, then /method.php?mode=ajax&output=json, then /passthrough.php?mode=ajax&output=json&path=method, all in one go? It seems to be running through the .htaccess once for each rule that is matched, instead of parsing the URL, changing based on the next rule, changing based on the next rule, and so on.

I've tried using several of the flags, incuding [PT] and [S], but they're poorly-documented. The issue can be solved by writing special conditions which pass the code directly to the passthrough script, but this is not well-broken out and does not 'feel' right to me.

A: 

Try this:

RewriteRule ^passthrough\.php$ - [L]
RewriteRule !\.php$ - [L]
RewriteRule ^ajax/(.*) passthrough.php?path=$1&mode=ajax [L,QSA]
RewriteRule ^(xml|json)/(.*) passthrough.php?path=$2&output=$1 [L,QSA]
Gumbo
This get close -- it removes the error with the path to the method all messed up, but doesn't do both the output format and ajax. I'll use this, then parse the path in PHP to get what it missed. Thanks!
stdave
A: 

Honestly, I think you're wrong to say that passing the PATH_INFO into passthrough.php and parsing it in your script. You're setting yourself up for a maintenance nightmare by (ab)using mod_rewrite the way you are.

womble
A: 

womble, do you have a better way to do things like this?

Use the comment function of the corresponding answer!
Gumbo