如何将所有传入请求重定向到https?

问题描述:

原谅我,但我一直在寻找答案。尝试和测试不同的东西,我似乎无法找到答案。如何将所有传入请求重定向到https?

我对我的域名拥有SSL,这就是我的.htaccess的外观。

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC] 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L] 

myexample.com和http://myexample.com重定向到https。唯一的问题是,当我键入www.myexample.com它不重定向到https也不打字http://www.myexample.com

任何人都可以帮助我或指向正确的方向吗?

您的RewriteCond只匹配以example.com开头的主机名。请尝试:

RewriteCond %{HTTP_HOST} ^example\.com [NC,OR] 
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] 

此外,www.example.com必须是基于名称的虚拟主机的ServerName或ServerAlias。