使用.htaccess密码保护目录及其所有子文件夹

xghobddn  于 2022-11-16  发布在  其他
关注(0)|答案(6)|浏览(102)

我试图密码保护一个子域和它的所有子目录和文件,但我的知识在这件事上是非常有限的,我该如何去做呢?

ix0qys7i

ix0qys7i1#

这是一个简单的两步过程
在您的.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>
ufj5ltwl

ufj5ltwl2#

要对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

eqoofvh9

eqoofvh93#

只是扩展马赫什的答案。

x1月0n1x日
AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user

如果您不想使用在线密码生成器,您可以使用htpasswdopenssl

1.使用htpasswd

htpasswd -c /path/to/the/directory/you/are/protecting/.htpasswd my_username
# then enter a password
# -c means Create a new file

2.使用openssl

openssl passwd -apr1 your_password

然后将生成的密码输入.htpasswd,格式为:

username:<generated_password>

示例:

.htpasswd
my_username:$apr1$ydbofBYx$6Zwbml/Poyb61IrWt6cxu0
d7v8vwbk

d7v8vwbk4#

您需要生成一个用于身份验证的密码(用户名+密码)字符串,将其写入一个文件,并将其放在要限制访问的子目录中。
字符串如下所示,

username:hashkey
  • 您可以使用HTTP password generator tool来执行此操作。
  • 将从上述站点获得的字符串复制并粘贴到站点webroot之外的任何位置的新文件(.htpasswd)中(最好保存在用户主目录内的任何位置)。
  • 在.htaccess文件中添加以下行。
AuthType Basic
AuthName "Require Authentication"
AuthUserFile [PATH_TO_FILE]/.htpasswd
Require valid-user
  • 如果密码未触发,请检查.htaccess文件的权限。
  • 如果验证失败,请检查指定位置是否存在.htpasswd文件。(确保您的用户帐户对.htpasswd文件具有足够的读取权限)
  • 您不需要重新启动服务器即可完成此操作。

如果您有任何疑问,请告诉我。

toe95027

toe950275#

Apache提供了一个非常好的guide模块来使用所有的身份验证和授权模块。

hivapdat

hivapdat6#

要创建一个正确的密码,您可以创建一个php文件,并在本地运行它(在您的计算机上,而不是在Web服务器上),其中包含以下内容:

<!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

相关问题