我在使用 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 上