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

Ody*_*3us 15 .htaccess password-protection

我正在尝试密码保护子域及其所有子目录和文件,但我对此事的知识非常有限,我该怎么做呢?

Thanx提前!

Mah*_*esh 22

这是一个简单的两步过程

在你的.htaccess中

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user
Run Code Online (Sandbox Code Playgroud)

使用http://www.htaccesstools.com/htpasswd-generator/或命令行生成密码并将其放入.htpasswd

注意1:如果您使用的是cPanel,则应在安全性部分"密码保护目录"中进行配置

编辑:如果这没有工作,那么propably你需要做AllowOverride All的的.htaccess的目录(或至少到以前的)的http.conf然后是阿帕奇重启

<Directory /path/to/the/directory/of/htaccess>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
</Directory>
Run Code Online (Sandbox Code Playgroud)

  • 我将以不同的方式表达:将.htpasswd放入一个未以任何方式暴露给Web的目录(受保护或不受保护),但是对于apache是​​可用的. (5认同)

小智 8

要对Apache提供的目录进行密码保护,您需要在要保护的目录中使用.htaccess文件,以及.htpasswd文件,该文件可以是您的系统上Apache用户可以访问的任何位置(但将其放在合理且私密的位置).你很可能不想把它放在.htpasswd同一个文件夹中.htaccess.

.htaccess文件可能已存在.如果没有,请创建它.然后插入:

AuthType Basic
AuthName "Your authorization required message."
AuthUserFile /path/to/.htpasswd
require valid-user
Run Code Online (Sandbox Code Playgroud)

然后使用您想要的任何用户名和密码创建.htpasswd文件.密码应加密.如果您使用的是Linux服务器,则可以使用htpasswd命令为您加密密码.以下是该命令如何用于此:

htpasswd -b /path/to/password/file username password


Nin*_*ham 5

只需扩展 Mahesh 的答案。

.htaccess

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user
Run Code Online (Sandbox Code Playgroud)

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

1. 使用 htpasswd

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/the/directory/you/are/protecting/.htpasswd
require valid-user
Run Code Online (Sandbox Code Playgroud)

2. 使用 openssl

openssl passwd -apr1 your_password
Run Code Online (Sandbox Code Playgroud)

然后把生成的密码改成.htpasswdwith 格式:

username:<generated_password>
Run Code Online (Sandbox Code Playgroud)

例子:

.htpasswd

my_username:$apr1$ydbofBYx$6Zwbml/Poyb61IrWt6cxu0
Run Code Online (Sandbox Code Playgroud)

  • 请注意,htpasswd 文件不需要命名为 .htpasswd,也不需要位于您要保护的文件夹中。为了安全起见,您可以将其放在 http / httpdocs 文件夹之外,这样就无法通过 HTTP/S 访问它。例如 /var/www/private-files/password-list-for-protected-folder (2认同)