Apache 2.2 重定向到 SSL *然后* 进行身份验证(使用解决方案 < 但这是废话吗?)

标签 apache mod-rewrite apache2 basic-authentication apache2.2

似乎是 apache 的地方,所以这里是 :)

古老的问题:我如何重定向 HTTP->HTTPS,然后并且仅当 HTTPS 时才进行身份验证?

哦 - 我希望它的大部分内容都在一个可以包含在多个 块中的片段中,所以没有基于虚拟主机级随机路径的重写......

好吧,这就是我所拥有的似乎确实有效的方法:

在 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

加号

Require 的所有条形都可以抽象为一个片段文件,以包含在每个位置的一行中

它修复了每个位置强制 SSL 和身份验证的问题,从而减少出错的机会

减分

该死的 hell ,这几乎是不直观的!据我所知,可能很脆弱......

有没有更好的方法(不是我发现的......)?

非常欢迎评论是否有任何严重缺陷:)

旁白
如果 Apache 有一个合理的配置语法和一个通用的



可以在任何地方使用的块。它具有某些特殊情况块,例如 IfModule,然后还有特殊情况条件,例如 RewriteCond(如果您不习惯,则很难理解)。

干杯,

蒂姆

最佳答案

如果您想强制整个站点使用 https,可以使用 虚拟主机 指令,然后很简单:

<VirtualHost *:80>
    ServerName example.com

    RedirectMatch (.*) https://example.com$1

</VirtualHost>

<VirtualHost *:443>
    ServerName example.com

    ...
    ...
    ...
</VirtualHost>

关于Apache 2.2 重定向到 SSL *然后* 进行身份验证(使用解决方案 < 但这是废话吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458398/

相关文章:

apache - Mod_Rewrite 意外行为 L 标志

Apache-ssl 与 mod_ssl

php - 在 MAC OS X 中通过终端重新加载 Apache2

configuration - Apache2:预期 </> 但看到 </VirtualHost>

linux - Apache - 无法在 var/log/httpd/site 文件夹中创建新的 access.log 文件

.htaccess - 将 http 重定向到 https 在子目录中出现问题 404 - 未找到

apache - RewriteRule 将 url 部分作为参数传递给 php 脚本

apache - DNS 问题 - dig 解决但 curl 无法连接到主机

ios - .htaccess 中重写文件的 MIME 类型不起作用

linux - Apache 一直要求 401 授权