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

yan*_*kee 12 apache webdav

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

Alias /mediamanager /storage/files/mediamanager
<Directory /storage/files/mediamanager>
  DirectoryIndex /mediaManagerIndex.php
  DAV On
  # ... And some authentication directives ... #
</Directory>
Run Code Online (Sandbox Code Playgroud)

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

这在Apache 2.2中运行得很好,但最近我升级到了Apache 2.4,现在它已经坏了.我非常怀疑我患有这个已经2岁并且没有修复的虫子.建议的解决方法添加:

<Limit PROPFIND>
  DirectoryIndex never-encounterable-file-name.html
</Limit>
Run Code Online (Sandbox Code Playgroud)

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

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

Dim*_*ski 5

要解决此问题,请禁用 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>
Run Code Online (Sandbox Code Playgroud)

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

sudo service apache2 reload
Run Code Online (Sandbox Code Playgroud)

  • 这将是一个可能的紧急解决方案,但它有一个缺陷,我需要告诉用户两个 URL:一个用于 WebDAV,另一个用于使用网络浏览器。(并且这在概念上是不必要的,这在 Apache 2.2 中确实有效。)。 (2认同)

小智 4

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

  • 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}/$1/index.php"  "-f"
RewriteRule  "^/(.*)/$"                 "/$1/index.php"  [L]
Run Code Online (Sandbox Code Playgroud)

不要忘记重新加载 Apache!