htaccess将域重定向到https,将子域重定向到https,将非域名重定向到www

seb*_*tvd 2 .htaccess redirect

我希望这样做:

强制https为我的主域名.

http到https:// www. http:// wwwhttps:// www.

但不适用于子域名

http://subdomain.domain.comhttps://subdomain.domain.com

有人可以帮助我,我找不到这个

Roh*_*ani 9

您可以在根目录中使用它.htaccess:

RewriteEngine on 

# redirect to https www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)

[编辑]

RewriteEngine on 

# redirect no-www to www only main domain, not with subdomain
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# redirect http to https all domain
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)