如果请求不是来自 localhost,则 Apache 重定向到 https

ant*_*iks 5 centos https rewrite apache-httpd virtual-host

我有一台 Centos 6.7 和 apache 2.2.15。我想将所有流量从端口 80 重定向到 443,但有一个例外。来自 localhost 的流量我想保留在端口 80 上。

现在我使用这个配置,但我不知道如何更改它

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
Run Code Online (Sandbox Code Playgroud)

Dan*_*Sut 3

如果您是否启用了 IPv6,则需要使用RewriteCondwith %{REMOTE_ADDR}(可能是多个)来处理。这是基本配置:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
Run Code Online (Sandbox Code Playgroud)

这是有关使用 RewriteCond 的教程,可能会有所帮助