将域和子域从 http 重定向到 https

Redirecting domain and subdomain from http to https

我想将我的域和子域的所有 http 请求重定向到 https。比如http://example.com should go to https://example.com and http://subdomain1.example.com should go to https://subdomain1.example.com,等等

我写过这段代码:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^(.*)$ https://%1.example.com [R=302,L]

此代码仅重定向子域,不重定向域。我应该对上面的代码做哪些更改才能开始路由。

您可以使用以下方法将您的域重定向到 https

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]