通过 htaccess 将 http 到 https 重定向到子域和 www 转换

http to https redirect with subdomains and www conversion via htaccess

我需要对特定子域的每次调用。example.com.xx在开头添加 www,然后将其重定向到 https。

在 there is a solution but does not work with sub.example.com.xx redirecting to https://www.sub.example.com.xx

RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如果我输入 http://sub.example.com.., I get https://sub.example.com.xx and not https://www.sub.domain.com.xx

即使 http://yyy.sub.example.com.xx must give me https://www.yyy.sub.example.com.xx

示例:

example.com.xx ---> https://www.example.com.xx
http://example.com.xx ---> https://www.example.com.xx
https://example.com.xx ---> https://www.example.com.xx

sub.example.com.xx ---> https://www.sub.example.com.xx
http://sub.example.com.xx ---> https://www.sub.example.com.xx
https://sub.example.com.xx ---> https://www.sub.example.com.xx

other.sub.example.com.xx ---> https://www.other.sub.example.com.xx
http://other.sub.example.com.xx ---> https://www.other.sub.example.com.xx
https://other.sub.example.com.xx ---> https://www.other.sub.example.com.xx

您可以使用:

# redirect to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

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

首先是 www,以避免双重重定向。