我需要在几个页面上设置身份验证,并在未授权的情况下转发用户回来。目前,它会显示包含错误的目标页面。
Unauthorized
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.我对基本身份验证和apache conf一无所知。我有谷歌深度,没有找到任何解决方案,请指教。
谢谢
SetEnvIf Request_URI ^/en auth=1
AuthName "Please login to access english part"
AuthType Basic
AuthUserFile "/path/to/my/.htpasswd"
# first, allow everybody
Order Allow,Deny
Satisfy any
Allow from all
Require valid-user
# then, deny only if required
Deny from env=auth发布于 2019-06-15 03:36:28
这实际上是HTTP基本身份验证的一个限制。
但是,您可以自定义401响应(我假设这就是您所看到的,而不是403)。但是,您可以直接从自定义401重定向,这将导致客户端收到3xx响应,而不是401,这对用户来说不是那么详细和令人困惑。或者,你展示一条“友好的”消息,并提供一个链接,指向它们的来源地。
额外的问题是知道将用户返回到哪个页面。除非您将此信息存储在会话中,否则您将需要检查Referer HTTP请求报头,该报头可能根本没有设置。
例如..。在.htaccess文件的顶部,为401响应定义您的自定义错误文档:
ErrorDocument 401 /errordocs/e401.php在/errordocs/e401.php中,您将拥有类似以下内容:
<?php
/**
* 401 Unauthorized - Error Document
*/
// Get the HTTP Referer (if set at all)
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
// Immediately redirect back to the referring page if known
// But the client then sees a 3xx response, without any error, which could be confusing for users
if (!empty($referer)) {
header('Location: '.$referer,true,302);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>401 Unauthorized</title>
</head>
<body>
<h1>401 Unauthorized</h1>
<p>Sorry, you do not have permission to view that resource.</p>
<p>
<?php if (empty($referer)): ?>
<a href="/">Go back to the home page</a>
<?php else: ?>
<a href="<?=$referer?>">Go back to <?=$referer?></a>
<?php endif; ?>
</p>
</body>
</html>要“自动化”这一过程,您也许可以在这么多秒之后实现元刷新(或JS“重定向”)返回到引用页面(如果设置)。
https://stackoverflow.com/questions/56584483
复制相似问题