Apache 模块 mod_dir

Description:提供“尾随斜杠”重定向并提供目录索引文件
Status:Base
Module Identifier:dir_module
Source File:mod_dir.c

Summary

目录的索引可以来自以下两个来源之一:

  • 用户编写的文件,通常称为index.htmlDirectoryIndex伪指令设置此文件的名称。由mod_dir控制。

  • 否则,服务器生成的列表。由mod_autoindex提供。

这两个功能是分开的,因此您可以根据需要完全删除(或替换)自动索引生成。

服务器收到对 URL http://servername/foo/dirname的请求(其中dirname是目录)时,将发出“尾随斜杠”重定向。目录要求末尾有斜杠,因此mod_dir发出了对http://servername/foo/dirname/的重定向。

DirectoryCheckHandler Directive

Description:切换配置另一个处理程序时此模块的响应方式
Syntax:DirectoryCheckHandler On|Off
Default:DirectoryCheckHandler Off
Context:服务器配置,虚拟主机,目录,.htaccess
Override:Indexes
Status:Base
Module:mod_dir
Compatibility:在 2.4.8 及更高版本中可用。 2.4 之前的版本隐式地充当好像指定了“ DirectoryCheckHandler ON”。

DirectoryCheckHandler指令确定当为当前 URL 配置了其他处理程序时,mod_dir是应检查目录索引还是添加斜杠。在按目录替换期间,可以通过诸如SetHandler之类的指令或通过诸如mod_rewrite之类的其他模块来设置处理程序。

在 2.4 之前的版本中,如果为 URL 配置了任何其他处理程序,则此模块不执行任何操作。即使为整个目录指定了SetHandler指令,这也可以提供目录索引,但是它也可能导致与mod_rewrite之类的模块发生冲突。

DirectoryIndex Directive

Description:Client 端请求目录时要查找的资源列表
Syntax:DirectoryIndex disabled | local-url [local-url] ...
Default:DirectoryIndex index.html
Context:服务器配置,虚拟主机,目录,.htaccess
Override:Indexes
Status:Base
Module:mod_dir

当 Client 端通过在目录名称末尾指定/来请求目录索引时,DirectoryIndex伪指令设置要查找的资源列表。 Local-url 是服务器上文档相对于所请求目录的(%编码)URL;它通常是目录中文件的名称。可能会提供几个 URL,在这种情况下,服务器将返回它找到的第一个 URL。如果不存在任何资源并且设置了Indexes选项,则服务器将生成其自己的目录列表。

Example

DirectoryIndex index.html

那么对http://example.com/docs/的请求将返回http://example.com/docs/index.html(如果存在),或者将列出目录(如果不存在)。

请注意,文档不需要相对于目录。

DirectoryIndex index.html index.txt  /cgi-bin/index.pl

如果目录中没有index.htmlindex.txt都将导致 CGI 脚本/cgi-bin/index.pl被执行。

单个参数“ disabled”阻止mod_dir搜索索引。如果参数“ disabled”在其之前或之后具有任何参数,即使它们也被“禁用”,也将按字面意义进行解释。

注意: same context中的多个DirectoryIndex指令将添加到要查找而不是替换的资源列表中:

# Example A: Set index.html as an index page, then add index.php to that list as well.
<Directory "/foo">
    DirectoryIndex index.html
    DirectoryIndex index.php
</Directory>

# Example B: This is identical to example A, except it's done with a single directive.
<Directory "/foo">
    DirectoryIndex index.html index.php
</Directory>

# Example C: To replace the list, you must explicitly reset it first:
# In this example, only index.php will remain as an index resource.
<Directory "/foo">
    DirectoryIndex index.html
    DirectoryIndex disabled
    DirectoryIndex index.php
</Directory>

DirectoryIndexRedirect Directive

Description:为目录索引配置外部重定向。
Syntax:DirectoryIndexRedirect on | off | permanent | temp | seeother | 3xx-code
Default:DirectoryIndexRedirect off
Context:服务器配置,虚拟主机,目录,.htaccess
Override:Indexes
Status:Base
Module:mod_dir
Compatibility:在 2.3.14 版和更高版本中可用

默认情况下,DirectoryIndex被选中并透明返回给 Client 端。 DirectoryIndexRedirect导致发出外部重定向。

参数可以是:

  • on:发出 302 重定向到索引资源。

  • off:不发出重定向。这是 mod_dir 的遗留行为。

  • permanent:向索引资源发出 301(永久)重定向。

  • temp:与on具有相同的作用

  • seeother:向索引资源发出 303 重定向(也称为“其他”)。

  • 3 xx 码:发出以所选 3xx 码标记的重定向。

Example

DirectoryIndexRedirect on

http://example.com/docs/的请求将返回到http://example.com/docs/index.html的临时重定向(如果存在)。

DirectorySlash Directive

Description:切换尾斜杠重定向打开或关闭
Syntax:DirectorySlash On|Off
Default:DirectorySlash On
Context:服务器配置,虚拟主机,目录,.htaccess
Override:Indexes
Status:Base
Module:mod_dir

DirectorySlash指令确定mod_dir是否应修正指向目录的 URL。

通常,如果用户请求的资源没有指向目录的末尾斜杠,则mod_dir会将其重定向到相同的资源,但是具有末尾斜杠是出于一些良好的原因:

  • 用户最终正在请求资源的规范 URL

  • mod_autoindex正常工作。由于它不会发出链接中的路径,因此它将指向错误的路径。

  • DirectoryIndex将仅对带有斜杠的目录进行评估。

  • html 页面内的相对 URL 引用将正常工作。

如果您不希望这种效果并且上述原因不适用于您,则可以关闭重定向,如下所示。但是,请注意,这样做可能会带来安全隐患。

# see security warning below!
<Location "/some/path">
    DirectorySlash Off
    SetHandler some-handler
</Location>

Security Warning

关闭结尾的斜杠重定向可能会导致信息泄露。考虑一种情况,其中mod_autoindex是活动的(Options +Indexes)并且DirectoryIndex被设置为有效资源(例如index.html),并且没有为该 URL 定义其他特殊处理程序。在这种情况下,带有斜杠的请求将显示index.html文件。 但是不带斜杠的请求将列出目录内容

还要注意,某些浏览器在发出重定向时可能会将 POST 请求错误地更改为 GET(从而丢弃 POST 数据)。

FallbackResource Directive

Description:为未 Map 到文件的请求定义默认 URL
Syntax:FallbackResource disabled | local-url
Default:disabled - httpd will return 404 (Not Found)
Context:服务器配置,虚拟主机,目录,.htaccess
Override:Indexes
Status:Base
Module:mod_dir
Compatibility:disabled参数在 2.4.4 和更高版本中可用

使用此方法可为未 Map 到文件系统中任何内容的任何 URL 设置处理程序,否则将返回 HTTP 404(未找到)。例如

FallbackResource /not-404.php

将导致not-404.php处理不存在的文件的请求,而对已存在文件的请求不受影响。

通常希望有一个文件或资源来处理对特定目录的所有请求,但与现有文件或脚本相对应的那些请求除外。这通常称为“前端控制器”。

在 httpd 的早期版本中,此效果通常需要mod_rewrite,并需要使用-f-d测试来确定文件和目录的存在。现在这仅需要一行配置。

FallbackResource /index.php

现有文件(例如图像,css 文件等)将正常提供。

如果不需要从父目录继承,请使用disabled参数禁用该功能。

在诸如* http://example.com/blog/之类的子 URI 中,此子 URI *必须作为本地 URL 提供:

<Directory "/web/example.com/htdocs/blog">
    FallbackResource /blog/index.php
</Directory>
<Directory "/web/example.com/htdocs/blog/images">
    FallbackResource disabled
</Directory>

后备处理程序(在上述情况下为/blog/index.php)可以通过服务器变量REQUEST_URI访问原始请求的 URL。例如,要在 PHP 中访问此变量,请使用$_SERVER['REQUEST_URI']