mod_rewrite:将 https://example.com 重定向至 https://sub.example.com

mod_rewrite:将 https://example.com 重定向至 https://sub.example.com

我在 Apache 服务器上运行一个域名 example.com,我想重定向每个调用到https://example.comhttps://sub.example.com

我有一个配置如下的虚拟主机:

<VirtualHost _default_:80>
        ServerName sub.example.com
        RewriteEngine On
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost _default_:443>
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^https://%{HTTP_HOST}/$1
        RewriteRule ^(.*)$ https://cloud.%{HTTP_HOST}/$1 [R=301,L]

        ServerName sub.example.com
        ServerAdmin gnarf
        DocumentRoot /var/www/subfolder
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
        SSLEngine on
        SSLCertificateFile    /etc/apache2/ssl/ssl.pem
        SSLCertificateKeyFile /etc/apache2/ssl/ssl.key
        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>
        BrowserMatch "MSIE [2-6]" \
                nokeepalive ssl-unclean-shutdown \
                downgrade-1.0 force-response-1.0
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
        <Directory /var/www/subfolder>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                Allow from all
                Satisfy Any
        </Directory>
</VirtualHost>
</IfModule>

我尝试将重写条件添加到 default-ssl 文件中,但是当我重新加载配置时出现此错误:

Reloading web server config apache2                                                                             
[Sat Mar 22 22:57:11 2014] [warn] _default_ VirtualHost overlap on port 443, the first has precedence

所以我删除了 default-ssl。如何让 apache 接受我的重写条件?

谢谢,

艾拉

编辑:

为了使我的问题更清楚..重新加载错误不是我真正的问题,它只是为了澄清我迄今为止的尝试;)

http://example.com <- shows /var/www content = OK
https://sub.example.com <- shows /var/www/sub content = OK
https://example.com <- also shows /var/www/sub content = Not OK, I want this rewritten to https://sub1.example.com

但我不知道如何以及在哪里写这个规则。

答案1

我猜测你的RewriteCond不匹配是因为:

  • 似乎RewriteCond不喜欢%{HTTP_HOST} 比较%{HTTP_HOST}

  • %{HTTP_HOST}变量仅包含主机(Host Header),不包含前缀https://

你应该试试 :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ https://cloud.%{HTTP_HOST}$1 [R=301,L]

相关内容