【发布时间】:2011-03-08 07:36:34
【问题描述】:
我正在尝试对子域及其所有子目录和文件进行密码保护,但我对此事的了解非常有限,我该怎么做?
【问题讨论】:
标签: .htaccess password-protection
我正在尝试对子域及其所有子目录和文件进行密码保护,但我对此事的了解非常有限,我该怎么做?
【问题讨论】:
标签: .htaccess password-protection
这是一个简单的两步过程
在你的 .htaccess 中
AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user
使用http://www.htaccesstools.com/htpasswd-generator/或命令行生成密码 并将其放入 .htpasswd
注意 1:如果您使用的是 cPanel,您应该在安全部分“密码保护目录”中进行配置
编辑:如果这不起作用,那么您可能需要对 http.conf 中的 .htaccess(或至少以前的目录)的目录执行 AllowOverride All,然后是 apache重启
<Directory /path/to/the/directory/of/htaccess>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
【讨论】:
.htaccess 文件和.htpasswd 文件都放在sudomain 的目录中?还是将.htpasswd 文件放在我想要保护的目录中,将.htaccess 放在子域的目录中?
要对 Apache 提供的目录进行密码保护,您需要在要保护的目录中有一个 .htaccess 文件和一个 .htpasswd 文件,该文件可以位于系统上任何 Apache 用户可以访问的位置(但将其放在合理且合理的位置)私人的)。您很可能不想将.htpasswd 与.htaccess 放在同一文件夹中。
.htaccess 文件可能已经存在。如果没有,请创建它。然后插入:
AuthType Basic
AuthName "Your authorization required message."
AuthUserFile /path/to/.htpasswd
require valid-user
然后使用您想要的任何用户名和密码创建一个 .htpasswd 文件。密码应加密。如果您在 Linux 服务器上,您可以使用 htpasswd 命令为您加密密码。以下是该命令如何用于此目的:
htpasswd -b /path/to/password/file username password
【讨论】:
只需扩展 Mahesh 的答案。
.htaccessAuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user
如果您不想使用在线密码生成器,可以使用htpasswd 或openssl:
htpasswd
htpasswd -c /path/to/the/directory/you/are/protecting/.htpasswd my_username
# then enter a password
# -c means Create a new file
openssl
openssl passwd -apr1 your_password
然后将生成的密码放到.htpasswd,格式为:
username:<generated_password>
例子:
.htpasswdmy_username:$apr1$ydbofBYx$6Zwbml/Poyb61IrWt6cxu0
【讨论】:
您需要生成一个密码(用户名+密码)字符串进行身份验证,将其写入文件并将其放在您要限制访问的子目录中。
字符串看起来像,
username:hashkey
AuthType Basic AuthName "Require Authentication" AuthUserFile [PATH_TO_FILE]/.htpasswd Require valid-user
如果密码没有触发,检查.htaccess文件的权限。
如果验证失败,请检查指定位置是否存在 .htpasswd 文件。 (确保您的用户帐户对 .htpasswd 文件有足够的权限来读取)
您无需重新启动服务器即可实现此目的。
如果您有任何疑问,请告诉我。
【讨论】:
您可能想要使用mod_auth_digest 模块。 Apache 提供了一个非常好的guide 来使用全系列的身份验证和授权模块。
【讨论】:
要创建正确的密码,您可以创建一个 php 文件并在本地(在您的计算机上,而不是在网络服务器上)运行它,其中包含以下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form method="post" accept-charset="utf-8">
<input type="text" name="clear"/>
<input type="submit" name="submit" value="generate" />
</form>
<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_POST['clear']) && $_POST['clear'] != '') {
$cl = $_POST['clear'];
$pw = crypt($cl, base64_encode($cl));
echo $pw;
}
?>
</body>
</html>
我通常将我的 .htpasswd 文件放在 webcontent 目录之外的名为 /htpasswd/ 的目录中,例如 AuthUserFile /home/www/usr122/files/htpasswd/.sportsbar_reports_htpasswd(而不是在 webcontent 文件夹中 /home/www/usr122/html/htpasswd/)并将 .htpasswd 文件重命名为它的用途,例如.sportsbar_reports_htpasswd
密码文件本身应该是这样的
testusername:dGATwCk0tMgfM
用户名是testusername,密码是testuserpassword
【讨论】: