强制 SSL 不适用于 .htaccess



我已经在我的.htaccess文件中尝试了以下所有方法。

#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteEngine On
#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine On
# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on
# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我在此服务器上有两个虚拟主机,但似乎都没有工作,我在 httpd.conf 文件中启用了 mod-rewrite。 有什么想法吗?

不建议使用mod_rewrite将 HTTP 重定向到 HTTPS。您应该在虚拟主机配置中使用重定向 SSL。

例:

<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName example.com
# server settings e.g ProxyPass or DocumentRoot
SSLEngine On
# other ssl settings
</VirtualHost>

最新更新