Meaning of apache2 CONTEXT_DOCUMENT_ROOT and CONTEXT_PREFIX?

▼魔方 西西 提交于 2019-12-07 18:23:28

问题


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 when DirectoryIndex or ErrorDocument call a CGI script.

  • CONTEXT_PREFIX appears to be the original REQUEST_URI, sans any query part, when DirectoryIndex or ErrorDocument 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?


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!