![Apache Virtualhost 将 HTTPS 非 www 重定向到 HTTPS www 不起作用](https://linux22.com/image/748570/Apache%20Virtualhost%20%E5%B0%86%20HTTPS%20%E9%9D%9E%20www%20%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0%20HTTPS%20www%20%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我在使用 Apache 时遇到问题。我想重定向:
http://www.example.com到https://www.example.com- 好的
http://example.com到https://www.example.com- 好的
https://example.com到https://www.example.com- 未重定向
这是我在 Apache 中的 vhost.conf 的设置:
<NameVirtualHost *:80>
<VirtualHost *:80>
ServerAdmin [email protected]
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
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
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
</VirtualHost>
有人能帮我解决这个问题吗?
答案1
我用的是这个:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
“RewriteCond %{HTTPS} off”无效,因为它已经在端口 443 上