我从stackoverflow(Enforce www and trailing slash with mod_rewrite htaccess on a multi-domain site)的问题中提取了以下代码,并直接从apache.org提取。
该方案包含三个要求:
我提出了以下条件:
# Enforce www, if no subdomain is given
RewriteCond %{HTTP_HOST} !^(beta|dev|mobile|www)\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI}/ [R]
# Enfore SSL for all Domains
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
上述规则按预期工作,但单个案例不起作用:
https://domain.tld/未正确重定向到https://www.domain.tld/
有人可以帮我这个吗?
答案 0 :(得分:2)
尝试用以下代码替换您的2个代码:
RewriteCond %{HTTP_HOST} !^(?:beta|dev|mobile|www)\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]