子域重定向到没有virtualhost的主域

时间:2015-06-30 18:33:24

标签: apache redirect

我在apache中设置了一个站点sub.example.com,端口80和443上有虚拟主机(参见下面的配置)。实际上,这些是服务器上唯一的2个虚拟主机。

但是,当我导航到sub.example.com时,浏览器会重定向到example.com,甚至会找到正确的文档根目录并呈现sub.example.come的登录页面,然后它会获得ssl错误,因为我只有sub.example.com的证书。

我尝试的第一件事是将example.com重定向回sub.example.com。我首先尝试使用.htaccess和Rewrite指令,它似乎没有任何效果。然后我尝试在apache配置文件中设置永久重定向,但这会导致重定向循环。

sub.example.com如何重定向到example.com,以及example.com如何才能找到文档根目录?

理想情况下,example.com根本不存在,但我对此至少感到满意,至少会将example.com重定向到sub.example.com

这是我的apache配置。 (使用ubuntu 12.04和apache 2.2)

ports.conf

NameVirtualHost xx.xx.xx.xx:80
NameVirtualHost xx.xx.xx.xx:443

Listen 80

<IfModule mod_ssl.c>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

虚拟主机配置

# redirect to https
<VirtualHost xx.xx.xx.xx:80>
    ServerName sub.example.com
    DocumentRoot /var/www/path/to/webroot
    RedirectPermanent / https://sub.example.com
</VirtualHost>

# https
<VirtualHost xx.xx.xx.xx:443>
    ServerName sub.example.com

    DocumentRoot /var/www/path/to/webroot

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory /var/www/path/to/webroot>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    SSLEngine on
    SSLCertificateFile    /etc/apache2/ssl/sub.example.com.crt
    SSLCertificateKeyFile /etc/apache2/ssl/sub.example.com.key
    SSLCertificateChainFile /etc/apache2/ssl/chain.crt

    LogLevel notice
    ErrorLog ${APACHE_LOG_DIR}/sub-example-error.log
    CustomLog ${APACHE_LOG_DIR}/sub-example-access.log combined
</VirtualHost>

1 个答案:

答案 0 :(得分:-1)

你已经放

ServerName sub.example.com 

两次

相关问题