簡體   English   中英

如何在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-Client和簡單的Web瀏覽器訪問文件,在這種情況下,PHP腳本會生成一些漂亮的目錄視圖。

這在Apache 2.2中運行得很好,但最近我升級到了Apache 2.4,現在它已經壞了。 我非常懷疑我患有這個已經2歲並且沒有修復的蟲子 建議的解決方法添加:

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

對我不起作用。 可能是因為我仍然想要一個目錄索引。 如果我完全刪除我的DirectoryIndex WebDAV再次工作(此目錄中不存在index.html或類似文件)但當然我沒有能力使用我的PHP文件作為目錄索引。 我試圖在<Limit GET>指定我的DirectoryIndex,但這沒有任何效果。

有沒有辦法讓Debian和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}/$1/index.php"  "-f"
RewriteRule  "^/(.*)/$"                 "/$1/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  ^(.*)$  $1index.html  [L]

為了始終啟動所請求的PHP文件,只需用PHP文件名替換最后一行的“index.html”:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM