Meaning of apache2 CONTEXT_DOCUMENT_ROOT and CONTEXT_PREFIX?

蹲街弑〆低调 提交于 2019-12-06 06:10:46

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