htaccess force https&万维网。在域名和域名上http子网站上没有www

时间:2015-05-08 05:37:21

标签: apache .htaccess mod-rewrite subdomain no-www

如何使用htaccess强制https&万维网。在域上,&子域名http没有www?

以下内容将http://sub.domain.com重定向到https://domain.com

# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]


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

1 个答案:

答案 0 :(得分:3)

您可以使用:

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

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

清除浏览器缓存后需要对此进行测试。