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

12

在我的 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,现在它无法正常工作。我非常怀疑我遭受了这个错误,这个错误已经存在两年了,而且看起来没有修复的迹象。所提出的解决方法是添加:

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

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

是否有任何方法在Debian上的Apache 2.4中同时使DAV和DirectoryIndex正常工作(如果无论如何都不改变源代码和重新编译)?


这可能不是针对您具体问题的解答,但另一种潜在解决此问题的方法是放弃Apache的WebDAV处理程序并切换到类似sabre/dav的东西。 - Evert
3个回答

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>

然后重新加载 Apache,您将不再有这个问题:

sudo service apache2 reload

但是如果用户使用普通的Web浏览器访问存储库,我希望拥有目录索引。 - yankee
没问题。只需在不同的端口上配置您的WebDAV即可。因此,端口:80仍将正常提供站点和目录索引。但是,您可以通过另一个端口(例如443)访问WebDAV。然后,在您的配置文件中,您将仅为该端口的虚拟主机添加“DirectoryIndex disabled”。 - Dimitar Darazhanski
2
那可能是一个应急解决方案,但它有一个缺陷,我需要告诉用户两个URL:一个用于WebDAV,另一个用于使用Web浏览器。(而这不是概念上必要的,这在Apache 2.2中得到了证明。) - yankee

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]

不要忘记重新加载Apache!


耶!太棒了!可行! - yankee

0

这是我目前正在使用的解决方案,位于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  ^(.*)$  $1index.html  [L]

为了始终启动所请求的PHP文件,只需将最后一行的“index.html”替换为PHP文件名即可:
RewriteRule  ^(.*)$  $1mediaManagerIndex.php  [L]

用户明确表示他想让DirectoryIndex由他的PHP处理。因此禁用它不是一个选项。 - Léa Gris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接