.htaccess不要求密码

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

AuthType Basic AuthName "Password Required" AuthBasicProvider file AuthUserFile /home/janeb/.htpasswd require valid-user 

/home/janeb/.htpasswd文件是:

 inb351:$apr1$Ya4tzYvr$KCs3eyK1O2/c7Q9zRcwn.. 

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

 UserDir public_html <Directory ~ "public_html/.*"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> 

我重新启动Apache。 我试图改变require valid-user require user inb351 。 仍然没有运气。 我也用AuthConfigAuthConfig Indexes尝试了AllowOverride 。 所以我不知道还有什么可以做的,是的,我尝试过的每一步我都重新启动了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> 

我怀疑apache用户不能读取/home/janeb/.htpasswd 。 检查Apache的错误日志。

这是我在configuration中看到的唯一错误,但这可能不是唯一的问题。 请提供完整的虚拟主机configuration。 我也build议你将validationconfiguration移出.htaccess文件 – 没有理由在那里。

编辑

.htaccess文件没有被应用的原因是因为AllowOverride All没有被应用到你的.htaccess文件所在的path。

.htaccess文件需要与<Directory>块同时应用 – 如果在<Directory ~ ...>块中指定了AllowOverride则应在应用.htaccess之后才会发生。 由于这不起作用, 文件特别警告它 :

AllowOverride仅在<Directory>部分中指定不带正则expression式的部分,不在<Location><DirectoryMatch><Files>部分中有效。

添加一个新的块到您的configuration允许您的.htaccess文件被使用:

 <Directory /home> AllowOverride All </Directory> 

根据你的configuration,你可能需要把你的.htaccess文件放在/home/janeb/public_html目录下,这是DocumentRoot如果我没有错的话。

你想要做什么?

在你的vhost声明中

 order allow,deny allow from all 

你已经表示,每个人都应该有权访问…这将意味着你的以下.htpasswd是无关的,因为它的优先。

相反,删除代码,并在您的.htaccess所有你需要的是…

 AuthType Basic AuthName "Password Required" AuthUserFile /home/janeb/.htpasswd Require valid-user 

.htpasswd文件可以在服务器上的任何地方,只要它可以被Apache运行(通常是www-data)。

如果你想结合IP限制和.htpasswd限制,那么你也可以使用..

 order deny,allow deny from all allow from 192.168.0.10 AuthType Basic AuthName "Password Required" AuthUserFile /home/janeb/.htpasswd Require valid-user Satisfy Any 

我在这里遇到了同样的问题。 原来public_html定义inheritance自服务器的顶层( <Directory /> )设置,它包含了AllowOverride None 。 把这个AuthConfig All (或者AuthConfig )就行了。