问题原文
原帖链接 · not2savvy · CC BY-SA 4.0
I've set up a download site using Apache's index listing feature and a custom header:
Options +Indexes
HeaderName /index_header.html
While the default header displays the path ("Index of ...") on top of the index listing, I can't find a way to display the path using a custom header.
However, when navigating through the site structure, it would be helpful to have the path displayed somewhere. Is it possible to get this done somehow?
Not counting script solutions with PHP etc., just Apache configuration.
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
I think the "Apache-way" would be to use Server-Side-Includes (SSI) (mod_includes). There does not appear to be a way to access this information with mod_autoindex, nor to output arbitrary variables (that is what SSI is for).
For example, in .htaccess:
# Enable SSI for .html files (or use .shtml files instead)
AddOutputFilter INCLUDES .html
# Parse files for SSI
Options +Indexes +IncludesNOEXEC
HeaderName /index_header.html
(NB: .shtml files are often already configured for SSI in the server config, so if you rename the header file to .shtml then you probably don't need the AddOutputFilter directive.)
In /index_header.html:
<h1>Index of <!--#echo var="REQUEST_URI" --></h1>
However, REQUEST_URI will include a trailing slash on the "directory name" eg. /foo/bar/, unlike the default "Index of /foo/bar", which omits the trailing slash. If you specifically want to remove the slash suffix then you could create your own environment variable in .htaccess. For example:
# Capture the directory name less the slash suffix from the URL
SetEnvIf Request_URI "^/(?:(.+)/)?$" DIRECTORY_NAME=/$1
Then use DIRECTORY_NAME in place of REQUEST_URI in the above SSI.
The seemingly complex regex ^/(?:(.+)/)?$ is to account for both the document root, which consists of just a single slash, and subdirectories that contain both a slash prefix and suffix. If you are only dealing with subdirectories then it could be marginally simplified to:
# Capture the subdirectory name less the slash suffix
SetEnvIf Request_URI "(.+)/$" DIRECTORY_NAME=$1
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
