想要使用 htaccess 将 TLD 域重定向到 https 上的子域。已尝试找到 htaccess RewriteRule Redirect 但无法正常工作

Want to redirect TLD domain to a subdomain on https with htaccess. Tried htaccess RewriteRule Redirect found but not working

我想通过 .htaccess 指令重定向所有这些:

http://domain.com 和 www.domain.com 和 http://子域名.domain.com

收件人:

https://子域.domain.com

请注意 "subdomain" 是一个唯一的子域,我不想重定向任何子域,而只想重定向一个子域。

我不熟悉 .htaccess 指令,所以我尝试了在此处或其他地方找到的所有 RewriteRule 或 Redirect 片段,但它不起作用。我一定错过了什么。

编辑:我用它在子域的 sur 目录中添加了一个 .htaccess,它现在可以工作了: 没关系,我在htaccess中添加了这个到子域的子目录中:

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

试试:

# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]

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