How are the Apache2 (2.4) CGI environment variables CONTEXT_DOCUMENT_ROOT
and CONTEXT_PREFIX
defined?
From experimentation, I've determined the following:
CONTEXT_DOCUMENT_ROOT
appears to be the full local path to the original request whenDirectoryIndex
orErrorDocument
call a CGI script.CONTEXT_PREFIX
appears to be the originalREQUEST_URI
, sans any query part, whenDirectoryIndex
orErrorDocument
have called a CGI script. (In these cases,REQUEST_URI
is set to the URI of the CGI script, rather than the original.)
However, I can't seem to find any official documentation from Apache on these variables. Does anyone here have a link to such documentation, or more authoritative knowledge to share?
CONTEXT_PREFIX
and CONTEXT_DOCUMENT_ROOT
tell you how apache used an Alias
directive (or similar feature - like mod_userdir
) to translate the URL path to the file system path. The file system path will end up pointing to the the file to be served or a cgi script to run.
So, if apache translates the URL:
http://host/_CONTEXT_PREFIX/path/file
to the file system path:
/_CONTEXT_DOCUMENT_ROOT/path/file
it implies there is an Alias
(or ScriptAlias
or similar mechanism such as mod_userdir
) like the following:
Alias /_CONTEXT_PREFIX /_CONTEXT_DOCUMENT_ROOT
The Alias
directive saves /_CONTENT_PREFIX in ${CONTEXT_PREFIX}
and /_CONTEXT_DOCUMENT_ROOT is saved in ${CONTEXT_DOCUMENT_ROOT}
.
Here is one example of using it. In a <Directory>
context RewriteRule
translates a path name relative to the directory to a absolute URL path, or an absolute file name (which must exist). So if you wanted to translate URL's that ended in .htm to .php, you would have to write it like this:
<Directory "/_CONTEXT_DOCMENT_ROOT">
<FilesMatch "*.htm">
RewriteEngine On
RewriteRule (.*)[.]html$ /_CONTEXT_DOCUMENT_ROOT/$1.php
</FilesMatch>
</Directory>
Now you can write it without repeating /_CONTEXT_DOCUMENT_ROOT
, but perhaps more importantly the file does not have to exist because we are rewriting it to a URL path:
<Directory "/_CONTEXT_DOCMENT_ROOT">
<FilesMatch *.htm>
RewriteEngine On
RewriteRule (.*)[.].* ${CONTEXT_PREFIX}/$1.php
</FilesMatch>
</Directory>
来源:https://stackoverflow.com/questions/25751834/meaning-of-apache2-context-document-root-and-context-prefix