My Apache skills are not the best, but I generally know what I'm doing. After searching around quite a bit, I've found a number of places where this problem has been encountered, but none where it has been solved. Oh right, I should tell you the problem:
I have a PHP script that handles all of my HTTP errors (e.g. 404 not found). This is done by including lines like
ErrorDocument 401 /http_error_handler
in my main http.conf file, where http_error_handler is an Alias to my PHP script.
Alias /http_error_handler /file/system/path/to/httpError.php
Using the $_SERVER['REDIRECT_STATUS'] variable, I can find out the HTTP status code and serve error messages accordingly. It works great on the main site.
However, I have a VirtualHost for an "admin" (admin.mysite.com) subdomain (that requies SSL, although I don't think that matters). There are two levels of basic http authentication protecting it. First, the entire subdomain requires authentication:
<Directory />
Order allow,deny
Allow from all
AuthGroupFile "/file/system/path/to/.htgroup"
AuthUserFile "/file/system/path/to/.htpasswd"
AuthName "Admin Area"
AuthType Basic
Require group admins
SSLRequireSSL
</Directory>
Then, a subdirectory (for higher level admins) requires another login:
<Directory "/file/system/path/to/subfolder">
Options Indexes
Order allow,deny
Allow from all
AuthGroupFile "/file/system/path/to/.htgroup"
AuthUserFile "/file/system/path/to/.htpasswd"
AuthName "Admin Area"
AuthType Basic
Require user matt
SSLRequireSSL
</Directory>
Now, if I login to the first level (the base subdomain URL), and request a page that does not exist, I get my custom 404 page, so that is working. If I try to login to the protected subfolder and the login fails, I get my custom 401 Auth Required page.
However if I try to login to the main subdomain and fail, I get the default Apache 401 page, plus this error message:
"Additionally, a 401 Authorization Required error was encountered while trying to use an ErrorDocument to handle the request."
So, it seems that Apache is trying to access the custom error document, but I'm not allowed to see it because I am not logged in! The custom 401 page works on the subfolder because I'm already logged in to the admin domain from which the error page appears to be served.
The only solution I can see to this problem is to only protect subfolders, and not the subdomain as a whole, and maybe adding an automatic redirect to one of the subfolders so that admins don't have to always type the folder name on the URL. But that kind of stinks in the sense that it's a hack. Can anyone think of the "right" way to make this work?