如何在 Apache 2.4 中使用 DAV 和 DirectoryIndex?

How to use DAV and DirectoryIndex in Apache 2.4?

在我的 apache 配置中,我有一个这样配置的虚拟主机:

Alias /mediamanager /storage/files/mediamanager
<Directory /storage/files/mediamanager>
  DirectoryIndex /mediaManagerIndex.php
  DAV On
  # ... And some authentication directives ... #
</Directory>

想法是有人可以通过 WebDAV 客户端和简单的 Web 浏览器访问文件,在这种情况下,一些漂亮的目录视图由 PHP 脚本生成。

这在 Apache 2.2 中运行良好,但最近我升级到 Apache 2.4,现在它坏了。我高度怀疑我患有 this bug,它已经 2 岁了,而且看不到修复方法。建议的解决方法添加:

<Limit PROPFIND>
  DirectoryIndex never-encounterable-file-name.html
</Limit>

对我不起作用。可能是因为我还想有一个目录索引。如果我完全删除我的 DirectoryIndex WebDAV 再次工作(此目录中不存在 index.html 或类似文件)但是我当然失去了使用我的 PHP 文件作为目录索引的能力。我试图在 <Limit GET> 中指定我的 DirectoryIndex 但这没有效果。

有没有办法让 DAV 和 DirectoryIndex 在 Debian 上的 Apache 2.4 中同时工作(如果可能的话,无需更改源代码和重新编译)?

为了解决这个问题,禁用 WebDAV 站点的目录索引。

在您的 sites-available/site.conf 文件中,将 DirectoryIndex disabled 添加到 <Directory> 声明中,如下所示:

    <Directory /path/to/my/webdav/dir>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride all
                    Require all granted

                    DirectoryIndex disabled
   </Directory>

然后只需重新加载 Apache,您将不再遇到该问题:

sudo service apache2 reload

对我来说,以下配置解决了这两个问题:

  • WebDAV 再次工作
  • 目录索引,如果用户使用 Web 浏览器访问存储库

它通过使用简单的重写规则手动实现目录索引功能来工作,这些规则仅适用于 GET 请求方法。

必须将以下代码放在 apache 配置文件中的服务器配置或虚拟主机上下文中。

# Turn off (automatic) Directory-Indexing 
DirectoryIndex disabled

RewriteEngine On

# Rewrite rules for the root directory
RewriteCond  "%{REQUEST_METHOD}"  "(GET)"
RewriteRule  "^/$"                 "/index.php"  [L]

# Rewrite rules for other sub-directories
RewriteCond  "%{REQUEST_METHOD}"  "(GET)"
# The following line checks, if the index.php file exists
RewriteCond  "%{DOCUMENT_ROOT}//index.php"  "-f"
RewriteRule  "^/(.*)/$"                 "//index.php"  [L]

别忘了重新加载 Apache!

这是我当前使用的解决方案,位于 WebDav 服务使用的目录树根部的 .htaccess 文件中。在这种情况下,我不使用 PHP,只使用 html 文件,但它可以很容易地适应:

# Turn off automatic directory indexing 
Options -Indexes 
DirectoryIndex disabled

# Redirect directory requests to index.html, only for GET requests
RewriteEngine On
RewriteCond  %{REQUEST_METHOD}  "GET"
RewriteCond  %{REQUEST_FILENAME}  -d
RewriteRule  ^(.*)$  index.html  [L]

为了始终启动请求的 PHP 文件,只需将最后一行的 "index.html" 替换为 PHP 文件名:

RewriteRule  ^(.*)$  mediaManagerIndex.php  [L]