首页 文章

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

提问于
浏览
3

我试图用目录保护代码保护我的网站 . 该网站是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 回答

  • 4

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

    可以有一些 work-around 类型的解决方案可以跳过http URL的BASIC auth,并且仅针对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 auth dialog is also shown only once .

相关问题