Apache rewrite – get original URL in PHP

It really depends on the PHP setup. With mod_php you oftentimes still have the original request path in REQUEST_URI. For CGI or FastCGI setups it is quite commonly REDIRECT_URL. You will have to check a phpinfo() page to be sure.

If you really can’t find anything that would help, then it’s time for cheating! You can adapt your RewriteRule like this to retain the original URL in an environment variable of your chosing:

RewriteRule ^(\w+)$   test.php?ref=$1    [E=ORIG_URI:/$1]

This would then be available as $_SERVER["ORIG_URI"], or you can just get it from the URI with $_GET[‘ref’].
But you would have to use this trick on all potential RewriteRules.

Leave a Comment