Apache – 在AUTH之前redirect到https

我有一个开发网站,需要用户名和密码(基本httpauthentication),用户才能看到该网站。 我想首先redirect到一个安全的协议,在用户可以用明文发送密码之前。 任何想法如何使用Apache做到这一点? 我可以访问conf和.htaccess文件。

最终结果将是:

http:// xxxx / – 当用户到这里时,他们立即被redirect到https:

https – 当用户到达这里时,会提示input用户名/密码。

您可以使用http虚拟主机上的redirect指令redirect到完成validation的https站点。 你也可以使用mod_rewrite来做redirect。 基本的事情就是不要在http虚拟主机上设置authentication,而是在HTTPS虚拟主机上进行authentication的所有事情都redirect。

无论您如何configurationSSL虚拟主机,我都会将此configuration用于非SSL虚拟主机:

<VirtualHost *:80> ServerName www.sitename.com ServerAlias sitename.com others-if-you-like.com ServerAdmin [email protected] RedirectMatch ^/(.*) https://www.sitename.com/$1 [L,R] </VirtualHost> 

为您的日志logging添加行,但是不需要其他东西。 一切将被redirect到https:// URL,并且SSL站点的.htaccess或其他访问控制的东西将不会被处理,直到redirect之后。

你也可以在这里看到这个博客条目,它解释了如何使用SSLRequireSSL来做到这SSLRequireSSL ,这是一个自定义的ErrorDocument 403指令,它指向一个redirect到正确的HTTPS url的perl脚本。

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

最终,我们明确要求使用SSL,并将Web应用程序的根作为403和404错误文档用于HTTPS。 所以当一个人通过HTTP(这是未经授权的,因此是403)或一个不存在的页面(404)访问任何页面时,他正被redirect到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:[email protected]/~WEBUSER/ ### @link http://stackoverflow.com/a/1163884/328272 ### FORCE SSL: Explicitly require the SSL certificate of a certain domain to ### disallow ie. unsigned certificates. ErrorDocument's 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 

如果我没有记错,那么在SSL会话build立之前,在基本的握手过程中完成http基本authentication。 因此,即使您login到SSL服务器,基本身份validation也是以纯文本forms完成的。 这是基本authentication的长期问题之一。 也许其他的http身份validation方法之一处理这个。 我已经看了很长时间了。