在https网站上两次要求密码的目录保护?

时间:2014-01-09 10:20:42

标签: php .htaccess password-protection

我正在尝试使用目录保护代码保护我的网站。该网站采用PHP格式。

我的网站有https,就像https://www.example.com

当我打开网站时,它要求输入两次用户名和密码。我认为它需要一次用于http,一次用于https。

任何人都可以帮我解决这个问题。

以下是我的.htaccess file code

options -multiviews
<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</IfModule>

AuthType Basic
AuthName "example.com"
AuthUserFile /www.example.com/web/content/.htpasswd
Require valid-user

<IfModule mod_security.c> 
   # Turn off mod_security filtering. 
   SecFilterEngine Off 
   # The below probably isn't needed, 
   # but better safe than sorry. 
   SecFilterScanPOST Off 
</IfModule>

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

您正在进行两次身份验证对话,因为Apache在mod_auth指令之前运行mod_rewrite指令。此外,HTTP URL中的authentication会话设置在HTTPS URL中无效,因此Apache也必须遵循HTTPS下的BASIC auth指令。

可以有一些work-around类型的解决方案可以跳过http URL的BASIC身份验证,只针对HTTPS执行,但不是直接的。

更新:这是一个解决方案,您可以使用它来避免两次显示BASIC身份验证对话。

RewriteEngine On 
RewriteBase /

# redirect to HTTPS and set a cookie NO_AUTH=1
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,CO=NO_AUTH:1:%{HTTP_HOST}]

# If cookie name NO_AUTH is set then set env variable SHOW_AUTH
SetEnvIfNoCase COOKIE NO_AUTH=1 SHOW_AUTH

# show Basic auth dialogue only when SHOW_AUTH is set
AuthType Basic
AuthName "example.com"
AuthUserFile /www.example.com/web/content/.htpasswd
Require valid-user
Order allow,deny
allow from all
deny from env=SHOW_AUTH
Satisfy any

由于cookie仅设置为http://domain.com,因此env变量也只设置一次, BASIC身份验证对话框也只显示一次

相关问题