htaccess将通配符子域重定向到https(包括www)

时间:2014-09-17 19:57:32

标签: php apache .htaccess mod-rewrite redirect

目前,我的htaccess文件会将所有子域的非https请求重定向到https ...除了www请求。即http://subdomain.example.com被重定向到https://subdomain.example.com(这是正确的),但www.subdomain.example.com被重定向到https://www.subdomain.example.com(不正确)。这是我的.htaccess代码:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Trying to redirect to https
    RewriteCond %{HTTPS} off
    RewriteCond %{SERVER_PORT} !=443
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

1 个答案:

答案 0 :(得分:3)

尝试更改此内容:

RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.+\.example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]