【发布时间】:2015-12-17 17:40:40
【问题描述】:
我正在使用带有 UBUNTU14 LTS 的 Apache2,所有“标准”服务器。
问题很简单,在使用http://www.example.com 时可以正常工作,但是在使用http://example.com 时却不行,它会进入错误的索引。
描述细节
我在ls /var/www/有文件夹
example.com example2.example html
wiki.example.com wp.example2.example
每个都有不同且良好的 index.htm 与 http://www.example.com 一起使用(转到 /var/www/example.com)或 http://wiki.example.com 等。但 http://example.com 的索引转到 /var/www/html/index.htm,而不是 /var/www/example.com(! )。
/etc/apache2/sites-available/example.com.conf 我们有,
<VirtualHost *:80>
ServerAdmin mymail@ggmail.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName wiki.example.com
DocumentRoot /var/www/wiki.example.com
</VirtualHost>
编辑(注释)
我们正在使用一种解决方法——请忽略 cmets 中的“真实世界”示例,它作为真实测试无效——一段难看的 PHP 代码(不是解决方案)根(无子域)的/var/www/html/index.php,重定向到www子域,
if ( preg_match( // if HTTP from other domain, redirects
'/(mydomain1|mydomain2|mydomain3)/',
strtolower($_SERVER['HTTP_HOST']),
$m
) ) switch($m[1]) {
case 'mydomain1':
header("Location: https://www.example.com"); die('');
case 'mydomain2':
header("Location: https://www.example2.example"); die('');
case '...':
header("Location: https://www...."); die('');
}
编辑
正如@covener 评论所建议的,apachectl -S,结果
VirtualHost configuration:
*:80 is a NameVirtualHost
default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost atarica.com.br (/etc/apache2/sites-enabled/atarica.com.br.conf:1)
alias www.atarica.com.br
port 80 namevhost wiki.atarica.com.br (/etc/apache2/sites-enabled/atarica.com.br.conf:13)
port 80 namevhost atarica.com.br (/etc/apache2/sites-enabled/fabrincantes.com.conf:1)
alias www.fabrincantes.com
port 80 namevhost yellowtown.org (/etc/apache2/sites-enabled/yellowtown.org.conf:1)
alias www.yellowtown.org
port 80 namevhost wiki.yellowtown.org (/etc/apache2/sites-enabled/yellowtown.org.conf:35)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
【问题讨论】:
-
如果您需要在现实世界中检查,请参阅http://www.fabrincantes.com(即具有良好索引)和我需要的http://fabrincantes.com same index.htm,但是转到
/var/www/html/index.htm -
最好使用其中之一,而不是同时使用。我不认为两者都有大的惩罚,最好是使用www版本并将非www版本重定向到它。
-
嗨@John,谢谢线索...是的,这是目标(!)我需要non-www=www,...但是为什么不工作? ?有一个
ServerAlias和一个ServerName,它们是正确的... -
另外,我会使用 abc 或 xyz,而不是 xxx 作为占位符...因为你知道,这个解释有点开放。 :-)
-
您在 sites-available/ 中显示了一个文件——没有迹象表明它已加载到您的配置中。试试 apachectl -S
标签: apache