编程语言
首页 > 编程语言> > php-目录保护在我的https网站上两次要求输入密码?

php-目录保护在我的https网站上两次要求输入密码?

作者:互联网

我正在尝试使用目录保护代码来保护我的网站.该网站使用PHP.

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

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

谁能帮我解决这个问题.

以下是我的.htaccess文件代码

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>

在此先感谢您的帮助.

解决方法:

因为Apache在mod_rewrite指令之前运行mod_auth指令,所以您两次获得身份验证对话.同样,在HTTP URL中设置的身份验证会话在HTTPS URL中也不再有效,因此Apache也必须遵循HTTPS下的BASIC auth指令.

可能有一些解决方法,可以跳过对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身份验证对话框也仅显示一次.

标签:password-protection,php,htaccess
来源: https://codeday.me/bug/20191030/1964496.html