【问题标题】:Can't get the custom message for Apache2 401 authorization error无法获取 Apache2 401 授权错误的自定义消息
【发布时间】:2015-09-01 08:01:49
【问题描述】:

我有一个共享托管服务器,其中每个子域都在其根文件夹中。 www.domain.com/www 文件夹中,subdomain.domain.com 子域在/subdomain 文件夹中等等。

我现在想要的是使用.htaccess 密码限制对subdomain.domain.com 的访问,但为没有密码的用户显示自定义消息。而且我无法让 Apache2 读取 401 错误文档。我发现了一些常见的故障排除说文件必须是可读的,在我的情况下它肯定是可读的。

因此,由于/subdomain 受到保护,因此我可以将身份验证文件放入此配置的唯一两个位置是在/www 文件夹下,或者在我拥有的根目录下(如/401.html)不知道这是否有任何区别。但在这两种情况下,这些文件夹显然是 Apache2 可读的,因为我正在使用它们,我正在使用 PHP 脚本的另一个(主)域,并且我在根目录中收到错误日志,文件权限与 on 404 文件(有效),并且所有者是相同的。

而且我不认为我的托管商禁止我使用自定义 401 错误文档(我已经成功使用自定义 404 和 500 文档),因为只有当我尝试指定 401 文档时,我才会在我的错误输出显示Additionally, a 401 Authorization Required error was encountered while trying to use an ErrorDocument to handle the request.。好像它正在尝试这样做,但还有别的东西在路上。

可能是什么,我应该尝试什么?

编辑:

这是.htaccess文件的内容:

Options +Indexes +FollowSymLinks
RewriteEngine On

RewriteRule ^([^\.]+)$ index.php?data=$1 [QSA,L]

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /subdomain/.htpasswd
AuthGroupFile /dev/null
require valid-user

ErrorDocument 404 /tpl/errors/404.php
ErrorDocument 500 /tpl/errors/500.php
ErrorDocument 401 /auth_alpha.html

我现在注意到的是,我的 404 消息是以http 地址开头的绝对路径,而不是文件系统的根目录,这意味着如果我启动 401 错误文档/,它会尝试从同一个域中读取它吗?这对我来说没什么意义,因为它应该是 Apache2 指令,而不是浏览器指令,对吧?无论如何,当我尝试使用../auth_alpha.html 而不是/auth_alpha.html 时,浏览器只会在页面上输出字符串../auth_alpha.html

【问题讨论】:

  • 你能显示你的 .htaccess 强制密码的内容吗
  • @anubhava 当然,它正在编辑中

标签: .htaccess apache2 .htpasswd


【解决方案1】:

您需要从身份验证中排除 ErrorDocument URL:

ErrorDocument 404 /tpl/errors/404.php
ErrorDocument 500 /tpl/errors/500.php
ErrorDocument 401 /auth_alpha.html

Options +Indexes +FollowSymLinks
RewriteEngine On

RewriteRule ^([^.]+)/?$ index.php?data=$1 [QSA,L]

SetEnvIfNoCase Request_URI ^/(auth_alpha\.html$|tpl/errors/) NO_AUTH

# force auth for everything except ErrorDocument URLs
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /subdomain/.htpasswd
AuthGroupFile /dev/null
Require valid-user
Satisfy    any
Order      deny,allow
Deny from  all
Allow from env=NO_AUTH

【讨论】:

  • 这行得通,但我只是想问你一个小问题。为什么上一个目录然后从不受密码保护的主域中指定文件不起作用,例如../www/auth_alpha.html?
  • 只有在错误文档指令中使用http://maindomain.com/...时才会切换到主域。
猜你喜欢
  • 2015-05-12
  • 2022-08-16
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 2019-10-21
相关资源
最近更新 更多