Apache-2.4

Apache Virtualhost 將 HTTPS 非 www 重定向到 HTTPS www 不起作用

  • February 15, 2020

我遇到了 apache 的問題。我想重定向:

http://www.example.com>到<https://www.example.com - 好的

http://example.com>到<https://www.example.com - 好的

https:// example.comhttps://www.example.com - 未重定向

這是我在 Apache 中的 vhost.conf 的設置:

&lt;NameVirtualHost *:80&gt;

&lt;VirtualHost *:80&gt;
   ServerAdmin user@example.com
   ServerName example.com
   ServerAlias www.example.com
   Redirect permanent / https://www.example.com/
   DocumentRoot /home/example/public_html
   ErrorLog /home/example/logs/error.log
   CustomLog /home/example/logs/access.log combined
&lt;/VirtualHost&gt;

&lt;VirtualHost *:443&gt;
   ServerAdmin user@example.com
   ServerName example.com
   ServerAlias www.example.com
   RewriteEngine On
   RewriteCond %{HTTPS} off [OR]
   RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
   RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
   DocumentRoot /home/example/public_html
   ErrorLog /home/example/logs/error.log
   CustomLog /home/example/logs/access.log combined
&lt;/VirtualHost&gt;

誰能幫我解決這個問題?

我用這個:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

“RewriteCond %{HTTPS} off”無效,因為它已經在埠 443 上

引用自:https://serverfault.com/questions/1003175