How to detect root folder in URI when using apache mod_alias and mod_rewrite?
How to detect 开发者_开发知识库root folder in URI when using apache mod_alias and mod_rewrite?
I have an website running on a sub folder in of the document root for example /var/www/this/an/other/dir in this folder I have a .ht_access file with mod_rewrite rules to redirect everything to index.php
# Do nothing if the file (s), link (l) or (d) exists
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# Redirect everything else to index.php
RewriteRule ^.*$ index.php [E=PORT:%{SERVER_PORT},NC,L]`
I also have set up an alias for easy access
alias /foo /var/www/this/an/other/dir
Now when I go to the address http://host.com/foo/bar/ how can I detect that /foo is the root folder of my URI?
Easiest way is to set an env variable directly from within Apache:
SetEnv AliasRoot /foo
You can then access it through the $_ENV
superglobal:
$root = $_ENV['AliasRoot'];
I don't think there is a reliable way to do this in pure PHP.
I don't quite understand your question.
root folder is always the same - /
. No need to determine it.
If you need virtual document_root - just use dirname($_SERVER["SCRIPT_FILENAME"])
精彩评论