将http://和http:// www重定向到https:// www,而不会影响网站上的其他子域

时间:2014-05-03 19:01:21

标签: php apache .htaccess mod-rewrite redirect

我的网站上有htaccess两个问题

  1. 重定向
  2.   

    http://example.com => https://www.example.com

         

    http://www.example.com => https://www.example.com

         

    https://example.com => https://www.example.com

    我的.htaccess:

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

    它适用于所有浏览器,除非我尝试在Mozilla Firefox上访问example.com,并重写为https://www.example.com/https://example.com/

    1. 我不希望htaccess影响我网站上的其他子域名。例如我不想要 http://subdomain.example.com要重定向。
    2. 我的网站支持通配符子域,因此我不会重写所有子域。

1 个答案:

答案 0 :(得分:1)

我想这就是你要找的东西:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

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

我不知道你提到的那个mozilla异常。可能是一个缓存问题?您需要仔细查看日志文件,也许您必须启用重写日志记录才能看到重写引擎内部究竟发生了什么......