使用www和没有www重写规则

时间:2014-03-03 23:59:05

标签: regex apache .htaccess mod-rewrite rewrite

我搜索了很多,但我找不到具体问题的答案。

如果他们输入以下内容之一,目前所有用户都会重定向到https://www.mydomain.com

  • http://mydomain.com(将http替换为https并在此处添加www)
  • http://www.mydomain.com(此处将http替换为https)
  • https://mydomain.com(在此处附加www)

在上述条件下,我的.htaccess中有此代码。

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

我无法弄清楚如何停止任何子域的重定向。例如如果键入以下内容,则不应将用户重定向到https

  • abc.mydomain.com(子域名可以是任何名称)
  • http://abc.mydomain.com

知道怎么做吗?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

此规则应该有效:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

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

答案 1 :(得分:0)

尝试:

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 

KJ

相关问题