在请求用户authentication之前,Apache .htaccessredirect到HTTPS

这是我的.htaccess

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} AuthUserFile /etc/hi AuthName "hi" AuthType Basic require valid-user 

它要求使用http进行用户authentication,这意味着密码将以纯文本forms发送。 它会比redirect到https版本并再次询问密码。

我该如何解决它?

我这样解决这个问题。 只允许非SSL,因为它将被重定向,然后需要SSL上的身份验证一次…

 SetEnvIf %{SERVER_PORT} ^80$ IS_NON_SSL RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} AuthUserFile /etc/hi AuthName "hi" AuthType Basic require valid-user Allow from env=IS_NON_SSL 

如果您正在运行Apache 2.4,则可以使用配置部分轻松解决此问题。

例如…

 # Redirect to HTTPS RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] # Authenticate users only when using HTTPS # Enable for <v2.4 # SSLRequireSSL # ErrorDocument 403 /secure-folder/ # Enable for >v2.4 <If "%{HTTPS} == 'on'"> AuthType Basic AuthName "Special things" AuthUserFile /etc/blah.htpasswd # Prevent this 'Require' directive from overriding any merged previously <IfVersion >= 2.4> AuthMerging And </IfVersion> Require valid-user # Enable for >v2.4 </If> 

非常感谢,Istador!

我的Apache版本是2.2(Synology NAS DSM 5.1),所以这两个版本无法工作:

 RewriteOptions Inherit IfVersion 

把他们(和版本> = 2.4)的部分了。 整件事开始为我工作。

对于这个话题有很多的建议,我花了两天的时间来尝试。

但只有这一个为我工作。

以下是我所做的:

 RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] AuthType Basic AuthName "private area" AuthUserFile /path/to/file/.htdigest Order Deny,Allow Deny from all Satisfy Any Allow from env=!HTTPS Require valid-user 

所以经过验证可以在Apache 2.2,Synology DSM 5.1上运行。

检查的解决方案https://stackoverflow.com/a/15940387/2311074在Ubuntu 16.04上的Firefox上工作,但在Win 7上不能在Firefox上工作。

如果你想保护你的文件夹https://yourdomain.com/securefolder,那么你需要在该文件夹中创建一&#x4E2A;.htaccess与以下内容:

 RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} SSLRequireSSL ErrorDocument 403 https://yourdomain.com/securefolder AuthType Basic AuthName "Admin" AuthUserFile /outside/your/www/folder/.htpasswd Require user admin Admin 

它的工作方式是,当您通过http://而不是https://调用网站时,它会将您重定向到错误页面。 诀窍是使用正确的链接与https://作为您的默认错误页面。

我们客户的webapp安装在他的webuser目录中。 授权是在mod_rewrite规则( https://serverfault.com/a/443185/253111 )之前处理的,我们无法获得接受的答案,所以mod_rewrite似乎不是一个选项。

最终,我们明确要求使用SSL,并将Web应用程序的根作为403和404错误文档用于HTTPS。 所以当一个人通过HTTP(这是未经授权的,因此是403)或一个不存在的页面(404)访问任何页面时,他正被重定向到ie。 https://DOMAIN.TLD/~WEBUSER/admin 。

这是.htaccess文件,在注释中有一些额外的信息。

 ### INFO: Rewrites and redirects are handled after authorisation ### @link https://serverfault.com/a/443185/253111 ### INFO: Log out of a HTPASSWD session ### This was not always possible, but Firefox and Chrome seem to end sessions ### when a new one is trying to be using ie.: ### https://logout:logout@DOMAIN.TLD/~WEBUSER/ ### @link http://stackoverflow.com/a/1163884/328272 ### FORCE SSL: Explicitly require the SSL certificate of a certain domain to ### disallow unsigned certificates, etc. ErrorDocument commands are used to ### redirect the user to an HTTPS URL. ### @link http://forum.powweb.com/showthread.php?t=61566 SSLOptions +StrictRequire SSLRequireSSL SSLRequire %{HTTP_HOST} eq "DOMAIN.TLD" ### HTPASSWD AUTHENTICATION AuthUserFile /var/www/vhosts/DOMAIN.TLD/web_users/WEBUSER/.htpasswd AuthType Basic AuthName "Hello" Require valid-user ### ERROR DOCUMENTS: Redirect user in case of a 403 / 404. ErrorDocument 403 https://DOMAIN.TLD/~WEBUSER/admin ErrorDocument 404 https://DOMAIN.TLD/~WEBUSER/admin 

Molomby的解决方案工作在2.4及更高版本,但不适用于当前的Debian版本2.2.22。

Ben的/ Chris Heald的解决方案在2.2.22中也不适合我,但这是由于不同的订单/满足配置。 这些设置已经改变了2.4和解决方案似乎与2.4和以上不兼容(重定向工作,但浏览器只是显示未经授权的错误,而不要求凭据)。

这两个解决方案的组合应该适用于低于和高于2.4的版本:

 RewriteEngine on RewriteOptions Inherit # rewrite rules from parent directories RewriteCond %{HTTPS} off RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] AuthType Digest AuthName "private area" AuthDigestProvider file AuthUserFile /path/to/file/.htdigest <IfVersion < 2.4> Order Deny,Allow Deny from all Satisfy Any # reset this to 'All' in custom <Files> and <Directory> directives that block access Allow from env=!HTTPS Require valid-user </IfVersion> <IfVersion >= 2.4> <If "%{HTTPS} == 'on'"> AuthMerging And Require valid-user </If> </IfVersion> 

要求:mod_rewrite,mod_auth,mod_digest,mod_version

我正在运行的Apache 2.2和上述解决方案都没有为我工作。 我在这里找到了一个解决方法。 基本上,您需要设置SSLRequireSSL,并在ErrorDocument中使用一些脚本语言将用户转发到HTTPS。 不幸的是,在我的情况下,只有在访问服务器上的特定文件时才起作用,如果只提供域,则不起作用。 这是我做的:

 AuthType Basic AuthName "Password Protected Area" AuthUserFile /my/path/to/.htpasswd #Require valid-user <FilesMatch "(^(?!ssl.php).*)"> SSLRequireSSL ErrorDocument 403 /ssl.php Require valid-user </FilesMatch> 

FileMatch中的正则表达式告诉apache SSLRequireSSL除了ssl.php之外的所有文件 – 如果用户尝试使用SSL访问,则将用户转发到ssl.php。

我的ssl.php看起来像这样:

 if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "" || $_SERVER['HTTPS'] == "off") { $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header("HTTP/1.1 301 Moved Permanently"); header("Location: $redirect"); exit; } 

现在发生了什么:

最后一点是我不满意,如果有人有解决方案,我会很高兴听到这个。 我试图解决这个问题的事情:

  • 将正则表达式更改为(^ $)|(^(?!ssl.php)。*)以明确地匹配空字符串。 不工作
  • 增加了一个重写规则来重写一个空字符串到index.php。 也不行。