.htaccess 不询问密码

Sar*_*aya 9 ubuntu .htaccess apache-2.2

我正在使用 Ubuntu 12.04 并尝试在带有 apache2 服务器的页面上使用 .htaccess。我的 .htaccess 文件如下所示:

AuthType Basic
AuthName "Password Required"
AuthBasicProvider file
AuthUserFile /home/janeb/.htpasswd
require valid-user
Run Code Online (Sandbox Code Playgroud)

/home/janeb/.htpasswd 文件是:

inb351:$apr1$Ya4tzYvr$KCs3eyK1O2/c7Q9zRcwn..
Run Code Online (Sandbox Code Playgroud)

和 /etc/apache2/sites-available/default 文件是:

UserDir public_html
<Directory ~ "public_html/.*">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all  
</Directory>
Run Code Online (Sandbox Code Playgroud)

我重新启动了apache。我尝试更改require valid-userrequire user inb351. 仍然没有运气。我也试过AllowOverrideAuthConfigAuthConfig Indexes。所以我不知道还能做什么,是的,我尝试过的每一步都重新启动了 apache。

编辑:确切的默认文件是:

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>
    UserDir disabled vmuser
    UserDir public_html
<Directory ~ "public_html/*">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
Run Code Online (Sandbox Code Playgroud)

Sha*_*den 12

我怀疑apache用户无法阅读/home/janeb/.htpasswd. 检查Apache的错误日志。

这是我在提供的配置中看到的唯一错误,但这可能不是唯一的问题;请提供您的完整虚拟主机配置。我还建议您将身份验证配置移出.htaccess文件 - 没有理由将其放在那里。

编辑

.htaccess未应用文件的原因是因为AllowOverride All未应用到.htaccess文件所在的路径。

.htaccess文件需要与<Directory>块同时应用- 如果AllowOverride<Directory ~ ...>块中指定,那么它应该在应用之后 发生.htaccess。由于这不起作用,文档特别警告它

AllowOverride 仅在<Directory>没有指定正则表达式的部分中有效,在<Location>,<DirectoryMatch><Files>部分中无效。

在您的配置中添加一个新块以允许使用您的.htaccess文件:

<Directory /home>
    AllowOverride All
</Directory>
Run Code Online (Sandbox Code Playgroud)