mod_rewrite with RewriteMap: External Rewriting Program
Using an external Programm do do rewriting – in fact it can do anything depending on the request.
First a non-mentioned fact: if you use an external rewriting program apache will spawn it as a child process. Yes, a deamon child spawned by mod_rewrite. Be shure to know what you do, otherwise you’ll do more harm than good.
First a simple rewrite Statement
RewriteEngine On
# define the rewrite map – videoMap will be spawned and used by rewrite rules. php-win does not open a window
RewriteMap videoMap “prg:c:/path/php-win.exe -c E:/path/to/php.ini/ E:/path/external_rewriteMap.php”
# match for /video/v= and throw anything after v= into the daemon child
RewriteRule ^/video/v=(.*) /vardump.php?param=${videoMap:$1} [NC]
# RewriteLock file is needed for process communication
RewriteLock rewriteLockFile.txt
The PHP file for it
<?php
// HERE BE DRAGONS
// as php runs as daemon, NEVER exit(), and note that input comes from STDIN as we are piping
while($input = trim(fgets(STDIN, 1024))) {
if ($input) {
fputs(STDOUT, decode($input) ); // IMPORTANT: write out result to STDOUT
}
fputs(STDOUT, “\n” ); // end processing for this line with linebreak
flush(); // flush sends the result to STDOUT, this is very important to, no buffering
}
function decode($encoded) {
// some code to decode encrypted request parameters
}
?>
Pitfalls for the php: if your php deamon exits, your rewritemap will return nothing. Make shure apache has a php child. Testing is a pain as you can use $argv[] while writing your php, but you need STDIN in the apache version. Make shure you pass the correct php ini when spawning the php process (-c path/to/php.ini/ )
wow, this is really a totally crazy way to beg for trouble. You really want to spend nights debugging some very hidden problems, don´t you?
I strongly recommend NEVER EVER to do something like this! mod_rewrite will certainly bite you in the ass if you think you can outsmart it this way! instead just use a simple rule that redirects all your query parameters to one dispatch.php file and do all the work there – I do not see any reason to spawn daemons of mod_rewrite.
BTW – if anybody decides not to use Apache but cherokee or anything else, your video distribution will be br0ken.
Just because it can be done, it does not mean it should be done.
I am aware that spawning php as external mod_rewrite program also spawns dragons (big ones).