Apache 2.2redirect到SSL *然后* auth(解决scheme<但它是废话?)

似乎是阿帕奇的地方,所以在这里:)

年龄老问题:我如何redirectHTTP-> HTTPS,那么只有HTTPS,做一个authentication?

噢 – 我想大部分内容可以包含在多个<directory>或<location>块中,所以没有虚拟主机级别的随机path重写…

那么,这是我的似乎工作:

在VirtualHost块的顶部

# Set ssl_off environment variable RewriteEngine on RewriteCond %{HTTPS} =on RewriteRule ^ - [E=ssl] 

在位置或目录块中

 RewriteEngine on # Case 1 redirect port 80 SSL RewriteCond %{HTTPS} !=on RewriteCond %{SERVER_PORT} =80 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301] AuthType Basic AuthBasicProvider external AuthExternal auth_pam AuthName "My Underpants" AuthzUnixgroup on Order Deny,Allow Deny from all Allow from env=!ssl Satisfy any Require group nice-users 

加号

所有这些条件的需求可以被抽象出一个片段文件包含在每个位置的一行

它修复了针对每个位置强制SSL和身份validation,从而减less错误的几率

血腥的地狱,这是不直观的! 我所知道的可能是脆弱的

有没有更好的方法(不是我find的…)?

如果有任何严重的缺陷,我们将非常欢迎评论:)

如果Apache有一个通用的configuration语法, Aside Life会容易得多

<If expression> </ If>

可以在任何地方使用的块。 它有一些特殊的情况下,如IfModule块,然后你有像RewriteCond这样的特殊情况下的条件(如果你不习惯的话,这是非常困难的)。

干杯,

蒂姆

如果你想强制整个网站https,你可以使用VirtualHost指令,然后它很简单:

 <VirtualHost *:80> serverName example.com RedirectMatch (.*) https://example.com$1 </VirtualHost> <VirtualHost *:443> serverName example.com ... ... ... </VirtualHost> 

Tim Watts的解决方案似乎对我最好,但需要一点调整。 另外我的情况稍有不同,我希望允许某些IP地址没有HTTP身份验证,但这只是增加了一个额外的线。

默认情况下,mod_rewrite不会从VirtualHost继承配置。

请参阅: http : //httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteoptions

我打算使用“RewriteOptions继承”,但似乎这适用于父子规则之后。 无论如何,我想到了一个不同的解决方案。

在我的SSL <VirtualHost>中,我有一行:

 SetEnvIf Request_URI "/" using_ssl=yes 

如果请求URI包含一个正斜杠(即所有的时间),这将设置using_ssl环境变量。由于我更喜欢​​使用无条件的SetEnv,但显然是这样:

这个指令设置的内部环境变量是在大多数早期的请求处理指令运行之后设置的,比如访问控制和URI到文件名的映射。 如果您正在设置的环境变量是输入到处理的早期阶段(如RewriteRule指令)的输入,则应该使用SetEnvIf设置环境变量。

(来源: http : //httpd.apache.org/docs/2.2/mod/mod_env.html#setenv )

我的容器内的配置如下所示:

 # Require a basic HTTP auth user AuthName "realm-name-goes-here" AuthType Basic AuthUserFile /var/www/etc/htpasswd Require valid-user # OR allow from non-SSL (which will be redirected due to mod_rewrite below!) Order Allow,Deny Allow from env=!using_ssl # OR allow from a trusted IP range # NB: This allows certain IPs without a username & password Allow from 192.168.0.0/16 Satisfy Any # Force a redirect to HTTPS RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=permanent] 

为了测试目的,您可能首先尝试使用“R”而不是“R =永久”。

希望这对其他人有用:)

我已经测试过你的解决方案,但没有奏效

经过漫长的搜索解决方案,搜索太多,发现总是相同的东西,这是行不通的,我终于做到了这一点:我使用SSLRequireSSLErrorDocument 403配置一个静态页面包含一个JavaScript代码(感谢这个博客页面 )。

/etc/apache2/conf.d.opt/redirect_if_not_https.conf

 SSLRequireSSL ErrorDocument 403 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\ <html><head>\ <title>403 Forbidden</title>\ <script language=\"JavaScript\">\ window.location='https://'+window.location.hostname+window.location.pathname;\ </script>\ </head><body>\ <h1>Forbidden</h1>\ <p>You don't have permission to access that resource using simple HTTP. Please use HTTPS instead.</p>\ </body></html>" 

(注意我创建了/etc/apache2/conf.d.opt/

并在应用程序conf中包含该文件(例如,在/etc/apache2/conf.d/trac.conf ):

 <LocationMatch "/trac"> # All the classical configurations here # ... Include conf.d.opt/redirect_if_not_https.conf </LocationMatch>