【发布时间】:2015-06-30 18:33:24
【问题描述】:
我在 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>
【问题讨论】: