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

林少峰*_*林少峰 3 apache https redirect reverse-proxy http

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

er-*_*han 7

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

<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://www.example.com/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

或者

<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent /login https://www.example.com/login
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用 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}/$1 [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
Run Code Online (Sandbox Code Playgroud)

正如我所说,Apache 建议使用 VirtualHost 配置。

示例取自:

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

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