【问题标题】:www.Example.com works but not root (Example.com)www.Example.com 有效但不是 root (Example.com)
【发布时间】: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


【解决方案1】:

试试这个:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>

然后,您只需为您的真实服务器配置创建另一个 部分,其中包含 ServerName www.example.com

另外,CNAME 的记录工作不会有替代方案吗? (我不确定)

【讨论】:

  • 这也是一种与我的 PHP 解决方法具有相同(错误)行为的解决方法。请求example.com/abc?xyz=1 将丢失...您能修复您的解决方案吗?
  • @PeterKrauss — Redirect 指令应保留重定向中路径部分之后的所有内容。
  • 谢谢@Quentin,你说得对。谢谢 Richasantos,好线索...调试过程是由 covener 的评论开始的(尝试apachectl -S),他们显示了错误(请参阅我的编辑和我的答案)。所以我没有给 Richasantos 赏金,但我拿了第二票,所以一半的赏金。
【解决方案2】:

对不起,那是我的“人为错误”,在阅读 file.conf 脚本时作为盲点,在我的编辑显示 apachectl -S 结果后这里普遍失明,错误就在那里(!见 fabrincantes.com 行) ,“BBB”配置上的错误名称“AAA”:

     port 80 namevhost AAA (/etc/apache2/sites-enabled/BBB.conf:1)
             alias BBB

所以我修复了我的 BBB.conf 的简单名称更正,它有一个 ServerName AAA 而不是“BBB”,

   <VirtualHost *:80>
    ...
    ServerName BBB
    ...
   </VirtualHost>

【讨论】:

    【解决方案3】:

    您需要编辑您的 /etc/hosts 文件,使其指向正确的链接。

    例如,我的本地主机上的配置是

    127.0.1.1 example.com www.example.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-16
      • 2016-01-16
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      相关资源
      最近更新 更多