将http://www.domain.com重定向到https://domain.com

时间:2015-02-25 23:47:00

标签: apache .htaccess

我有以下apache虚拟主机:

NameVirtualHost  *:443

<VirtualHost *:80>

    <Directory /home/www/htdocs/domain.tl/html/>

        RewriteEngine On

        RewriteCond %{HTTP_HOST} ^domain.tl$ [NC]
        RewriteRule ^(.*)$ http://fr.domain.tl/$1 [L,R=301]

        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    </Directory>

</VirtualHost>

<VirtualHost *:443>

    ServerName domain.tl
    ServerAlias www.domain.tl fr.domain.tl en.domain.tl files.domain.tl
    DocumentRoot /home/www/htdocs/domain.tl/html/

    ...

    <Directory /home/www/htdocs/domain.tl/html/>

        Options -Indexes FollowSymLinks
        AllowOverride All

        RewriteEngine On

        RewriteCond %{HTTP_HOST} ^domain.tl$ [NC]
        RewriteRule ^(.*)$ https://fr.domain.tl/$1 [L,R=301]

    </Directory>

</VirtualHost>

我想将http://www.domain.tlhttp://domain.tl重定向到https://fr.domain.tl

实际上,使用此虚拟主机,http到https的重定向是正确的,但子域不会更改

1 个答案:

答案 0 :(得分:0)

尝试更改这些行:

    RewriteCond %{HTTP_HOST} ^domain.tl$ [NC]
    RewriteRule ^(.*)$ http://fr.domain.tl/$1 [L,R=301]

    RewriteCond %{HTTP_HOST} ^(?:www\.)domain.tl$ [NC]
    RewriteRule ^(.*)$ https://fr.domain.tl/$1 [L,R=301]

这样它也匹配“www.domain.tl”并在两个虚拟主机中重定向到https: