DirectoryIndex 在 htaccess 中有效,但在 apache2 .conf 檔案中無效

DirectoryIndex 在 htaccess 中有效,但在 apache2 .conf 檔案中無效

我需要安裝 Ubuntu 以使用 index.php 作為預設檔。

添加

  DirectoryIndex index.php 

到 /etc/apache2/apache2.conf

添加

<Directory /path/to/website/root/>
    DirectoryIndex index.php 
</Directory>

到 /etc/apache2/sites-available/mysite.conf 然後啟用並重新啟動

sudo a2ensite mysite
sudo service apache2 restart

我錯過了什麼?儘管如此,伺服器不會載入預設檔。

注意:添加DirectoryIndex index.php到 .htaccess 會產生所需的效果,但我更願意透過 .conf 檔案來執行此操作。

答案1

你把 DirectoryIndex 放在哪裡了?將其加到 .htaccess 相當於將語句放入:

<Directory /path/to/htaccess/file/>
DirectoryIndex index.php
</Directory>

因此,請嘗試在 DirectoryIndex 周圍新增目錄語句。有點黑客,但它可能會起作用嗎?如果它不嘗試向我們提供更多您的配置,例如整個虛擬主機配置。

答案2

今天,我們在使用 Apache 2.2 的 SLES 11 SP4 上遇到了類似的問題,並且使用了幾乎預設的 apache 配置。問題是,當未在 url 上明確呼叫時,Apache 會忽略現有的 index.html 檔案。例如 server.domain.tld/dir/

但是,如果使用顯式文件調用,它將提供內容。例如 server.domain.tld/dir/index.hmtl 。所以檔案系統權限沒有問題。 htdocs 下的所有檔案對於 OTHERS 群組都有足夠的權限。但當時所有的 htdocs 檔案都歸 root.root 所有。因此,在這種「模式」下,如果 URL 中未提及,Apache 不會提供 index.html。

當我們遞歸地將 htdocs 目錄和檔案改回所有者「wwwrun.www」(SLES(suse linux enterprise Server)上的「預設」)而不更改權限本身時,apache 開始像往常一樣再次提供 index.html預期的。無需重新啟動,也無需執行任何操作。

無法判斷這是一個錯誤還是一個功能。只是想讓您知道我們的經驗以及解決問題的方法。

因此,您可以檢查 htdocs 檔案的所有權,並確保其歸網路伺服器服務帳戶和群組所有。

答案3

嘗試改用位置指令。

<Location /path/to/htaccess/file/>
    DirectoryIndex index.php
</Location>

相關內容