Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DAV and DirectoryIndex in Apache 2.4?

Tags:

apache

webdav

In my apache configuration I have a virtual host configured like this:

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

The idea is that someone can access the files both by a WebDAV-Client and also a simple web browser in which case some pretty directory view is generated by a PHP script.

That worked great in Apache 2.2, but recently I upgraded to Apache 2.4 and now it is broken. I highly suspect I I suffer from this bug which is already 2 years old and no fix in sight. The proposed workaround to add:

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

Does not work for me. Probably because I still want to have a directory index. If I remove my DirectoryIndex altogether WebDAV works again (no index.html or similar files exists in this directory) but of course I loose the ability to use my PHP file as directory index. I tried to specify my DirectoryIndex in a <Limit GET> but this had no effect.

Is there any way to get both DAV and DirectoryIndex to work simultaneously in Apache 2.4 on Debian (if anyhow possible without changing the source code and recompiling)?

like image 879
yankee Avatar asked Jun 18 '15 13:06

yankee


Video Answer


2 Answers

In order to fix this, disable directory indexing for the WebDAV site.

In your sites-available/site.conf file add DirectoryIndex disabled to the <Directory> declaration, like so:

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

                    DirectoryIndex disabled
   </Directory>

Then just reload Apache and you will no longer have that issue:

sudo service apache2 reload
like image 198
Dimitar Darazhanski Avatar answered Oct 09 '22 08:10

Dimitar Darazhanski


For me, the following configuration solved both problems:

  • WebDAV works again
  • directory indexing, if the user uses a web browser to access the repository

It works by manually implementing the directory-indexing feature with simple rewrite rules, which are applied only for the GET request method.

The following code has to be placed inside the server config or virtual host context in the apache configuration file.

# 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]

Don't forget to reload Apache!

like image 34
Bernd T-Man Avatar answered Oct 09 '22 08:10

Bernd T-Man