如何在 apache 反向代理服务器上强制将 http 重定向到 https?

How to force redirect http to https on a apache reverse proxy server?

我有一个带有 http 和 https 服务的 apache 反向代理服务器。我想将 http 重定向到 https 强制。我应该如何配置配置文件?

推荐且更安全的方法是使用 VirtualHost:

<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent /login https://www.example.com/login
</VirtualHost>

另一种方法是使用 mod_rewrite:

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

正如我所说,Apache 推荐使用 VirtualHost 配置。

示例取自:

https://wiki.apache.org/httpd/RedirectSSL

https://wiki.apache.org/httpd/RewriteHTTPToHTTPS