apache在https中从无www重定向到www域

apache在https中从无www重定向到www域,apache,redirect,http-host,Apache,Redirect,Http Host,编辑: 永久重定向“/”https://%{HTTP\u HOST}/”在第一次重定向中实际上不起作用。这似乎是因为我的CDN自动将http重定向到https:,而不是因为我的设置。很抱歉给你带来了困惑 我正在尝试编写apachehttpd配置文件以将所有http流量重定向到https,并且还需要将none-www域重定向到www 例如 http://example.com http://www.example.com https://example.com 是否应全部重定向到

编辑:
永久重定向“/”https://%{HTTP\u HOST}/”
在第一次重定向中实际上不起作用。这似乎是因为我的CDN自动将http重定向到https:,而不是因为我的设置。很抱歉给你带来了困惑


我正在尝试编写apachehttpd配置文件以将所有http流量重定向到https,并且还需要将none-www域重定向到www

例如
http://example.com   http://www.example.com   https://example.com   

是否应全部重定向到
https://www.example.com

以下是我所做的:

<VirtualHost ipaddress:80>  
  ServerName example.com  
  ServerAlias www.example.com  

  Redirect permanent "/" "https://%{HTTP_HOST}/"
</VirtualHost>

ServerName example.com
ServerAlias www.example.com
重定向永久“/”https://%{HTTP_HOST}/“
这部分是有效的。所有http都重定向到https。注意,%{HTTP_HOST}已解析并替换。 现在我需要将none www重定向到www

<VirtualHost ipaddress:443>
  SSLEngine on
  ServerName www.example.com
  ServerAlias example.com

  <If "%{HTTP_HOST} !~ /www/">
     Redirect permanent "/" "https://www.%{HTTP_HOST}/"
  </If>

  ...
</VirtualHost>

斯伦金安
服务器名www.example.com
ServerAlias example.com
重定向永久“/”https://www.%{HTTP_HOST}/“
...
当我访问
https://example.com
它会重定向,但这次会重定向到
https://www.%{HTTP\u HOST}/
而不是
https://www.example.com/

现在如果我使用
重定向永久“/”https://www.example.com/“

然后
https://example.com
将被重定向到
https://www.example.com

因此,第二个VirtualHost块中的%{HTTP_HOST}似乎没有被替换


如何使其正确?

您可以尝试使用重写规则

 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^https://example.com [NC]
 RewriteRule ^(.*)$ https://www.example.com/$1