大网数据广告活动
网站运营与故障问答

Is it possible to display the path with an Apache index listing with a custom header?

· 52CE 我爱测

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…

问题原文

原帖链接 · 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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-06-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。