如何在Apache反向代理服务器上强制将HTTP重定向到HTTPS?

5

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

1个回答

11

推荐且更安全的方式是使用虚拟主机:

<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}/$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

正如我所说,Apache建议使用虚拟主机配置。

示例摘自:

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

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


谢谢,它能够正常工作了。我之前使用的是原始的ProxyPass,但效果不好。 - 林少峰

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接