views:

449

answers:

5

Say I have a rewrite that needs to pass the url to a PHP function and retrieve a value in order to tell what the new destination should be? is there a way to do that?

Thanks.

UPDATE:

Thanks so far guys..I am still having trouble but i'll show you my code:

.htaccess

#I got the file path by echoing DOCUMENT_ROOT and added the rest.
RewriteMap fixurl prg:/var/www/vhosts/mydomain.com/httpsdocs/domain_prototype/code_base/url_handler.php

RewriteEngine On
RewriteRule (.*) ${fixurl:$1} [PT]

PHP:

set_time_limit(0); # forever program!
$keyboard = fopen("php://stdin","r");
while (1) {
        $line = trim(fgets($keyboard));
        print "www.google.com\n";  # <-- just test to see if working.
}

However I am getting a 500 Internal Server Error I am not sure if there is an error in my .htaccess or in my PHP?

+3  A: 

Yes; you need a RewriteMap, of the External Rewriting Program variety.

chaos
+9  A: 

There is something called a RewriteMap.

You can call an executable script that will return the URL to rewrite to.

Check this article for more information and examples (in Perl, but are totally applicable to any other language):

http://www.onlamp.com/pub/a/apache/2005/04/28/apacheckbk.html

Summary of caveats:

  • Must run a read STDIN loop (ie, don't exit after receiving an URL)
  • Must print the URL to rewrite to WITH a trailing newline
  • Must be readable and executable by the user Apache runs as

This is the way to create the map

RewriteMap fixurl prg:/usr/local/scripts/fixit.php

And now we can use it in a RewriteRule:

RewriteEngine On
RewriteRule (.*) ${fixurl:$1}

EDIT: About the Internal Server Error. The most probable cause is what Gumbo mentions, RewriteMap cannot be used in .htaccess, sadly. You can use it in a RewriteRule in .htaccess, but can only create it in server config or virtual host config. To be certain, check the error log.

So, the only PHP / .htaccess only solution would be to rewrite everything to a certain PHP program which does the checking and redirects using the Location header. Something like:

RewriteRule (.*) proxy.php?args=$1 [QSA]

Then, in proxy.php

<?php
      $url = get_proper_destination($QUERY_STRING); #args will have the URI path, 
                                                    #and, via QSA you will have
                                                    #the original query string
      header("Location: $url");
?>
Vinko Vrsalovic
+1  A: 

What I would do is pass the URL to the PHP page and then do the final redirect using the PHP headers option.

header("Location: new-page.php");

If you didn't want it to redirect, you could also do an include of the page you want. I find that this is a little more flexible than using RewriteMap.

Hope this helps!

Daniel
+1  A: 

Note that the RewriteMap directive can only be used in the server config or virtual host context.

Gumbo
A: 

I am too having a problem in rewriting...so i tried the same code but i m facing the same problem.. so what is the solution?