通过htaccess

时间:2017-12-01 19:34:17

标签: .htaccess redirect https subdomain

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

htaccess redirect to https://www有一个解决方案,但无法使用sub.example.com.xx重定向到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 ..,我会https://sub.example.com.xx而不是https://www.sub.domain.com.xx

即使http://yyy.sub.example.com.xx必须提供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

1 个答案:

答案 0 :(得分:1)

您可以使用:

# 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首先,避免双重重定向。