Got myself stuck with the same problem today, and it seems we still don't have such method or variable available some 11 years later ... so here is a solution to compute it using mod_rewrite
Simply put those lines in your .htaccess
file :
# add some nasty folder name in request path
RewriteRule ^/?(.*)$ __PWD__/$1 [QSA]
# split to identify current directory and relative file
RewriteCond "%{REQUEST_FILENAME}" ^(.*/)__PWD__/(.*)$
# define environment variables and restore original path
RewriteRule ^ %1%2 [QSA,E=dir:%1,E=req:%2]
This code will set 2 environment variables :
- dir: full path to current folder
- req: subsequent request filepath
Afterwards, you can use those with %{ENV:dir}
and %{ENV:req}
but beware they don't get re-computed automatically!
Reminder: be sure you have mod_rewrite
enabled and RewriteEngine On
somewhere before the suggested code!
Example usage:
RewriteCond "%{ENV:dir}API/%{ENV:req}.php" -f
RewriteRule ^(.*)$ API/$1.php [QSA]
Or using full path:
RewriteCond "%{ENV:dir}API/%{ENV:req}.php" -f
RewriteRule ^ "%{ENV:dir}API/%{ENV:req}.php" [QSA]
Know you can update 'req' variable together with rewriting as follow:
RewriteCond "%{ENV:dir}API/%{ENV:req}.php" -f
RewriteRule ^(.*)$ API/$1.php [QSA,E=req:API/$1.php]
Same using environment variables:
RewriteCond "%{ENV:dir}API/%{ENV:req}.php" -f
RewriteRule ^ API/%{ENV:req}.php [QSA,E=req:API/%{ENV:req}.php]