将非 www(http) 和 www(http) 重定向到 https://www

Redirect non-www(http) and www(http) to https://www

我正在尝试从非 www(http) 和 www(http) 重定向到 https://www 但它不起作用。需要以下工作。请帮忙

  1. http://example.com redirect to https://www.example.com
  2. http://www.example.com redirect to https://www.example.com

当前的 htaccess 文件

rewritecond %{http_host} ^example.com [nc]

rewriterule ^(.*)$ https://www.example.com/ [r=301,nc]

尝试了以下规则

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/ [L,R=301]

你可以使用这个:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [NE,L,R=301]