2010-12-02 133 views
9

這裏的.htaccess文件的相關部分:自定義錯誤頁401

AuthUserFile /var/www/mywebsite/.htpasswd 
AuthGroupFile /dev/null 
AuthName protected 
AuthType Basic 
Require valid-user 

ErrorDocument 400 /var/www/errors/index.html 
ErrorDocument 401 /var/www/errors/index.html 
ErrorDocument 403 /var/www/errors/index.html 
ErrorDocument 404 /var/www/errors/index.html 
ErrorDocument 500 /var/www/errors/index.html 

Docuement根設置到/ var/WWW/mywebsite /網絡,這是許多虛擬主機的。我可以導航到index.html頁面。

我看到的全部是通用的Apache 401頁面,任何想法。

編輯:這是我的瀏覽器的錯誤信息:

Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

Additionally, a 401 Authorization Required error was encountered while trying to use an ErrorDocument to handle the request. Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny8 with Suhosin-Patch Server at www.dirbe.com Port 80

回答

7

確保在/ var/WWW /錯誤是由Apache用戶可讀的,包括這在你的Apache配置:

<Directory /var/www/errors> 
    Order allow,deny 
    Allow from all 
</Directory> 
+1

不幸。沒有任何錯誤日誌。我在錯誤頁面上看到了這一點:「此外,嘗試使用ErrorDocument來處理請求時遇到401需要授權錯誤。」 – 2010-12-03 00:32:17

+0

嘗試在部分中放入您需要的授權線路。 – 2010-12-03 00:58:15

3

這個問題(以及答案和評論)幫助了我很多,非常感謝!

我解決了一個稍微不同的方式,並希望分享。在這種情況下,我們需要提供一個自定義的401錯誤文檔,並且需要將根路徑代理到後端應用程序。

因此,例如,http://example.com需要服務於http://internal-server:8080/的內容。此外,http://example.com需要使用基本身份驗證和自定義401錯誤文檔進行保護。

因此,我在DocumentRoot中創建了一個名爲「error」的目錄。以下是來自vhost的相關行:

ErrorDocument 401 /error/error401.html 

    # Grant access to html files under /error 
<Location "/error"> 
Options -Indexes 
Order Deny,Allow 
Allow from all 
</Location> 

    # restrict proxy using basic auth 
<Proxy *> 
Require valid-user 
AuthType basic 
AuthName "Basic Auth" 
AuthUserFile /etc/apache2/.htpasswd 
</Proxy> 

    # Proxy everything except for /error 
ProxyRequests Off 
ProxyPass /error ! 
ProxyPass/http://internal:8080/ 
ProxyPassReverse/http://internal:8080/ 
0

ErrorDocument接受絕對URL路徑而不是文件路徑。所以它應該是:

ErrorDocument 404 /error/error.html 

假設在您的文檔根目錄下是/error/error.html文件。