Apache 默认为一个虚拟主机,而不是其他虚拟主机的根文件夹

Posted

技术标签:

【中文标题】Apache 默认为一个虚拟主机,而不是其他虚拟主机的根文件夹【英文标题】:Apache defaulting to one virtual host instead of root folder of the other vhosts 【发布时间】:2016-10-12 22:27:12 【问题描述】:

我在本地 Windows 机器上的 xampp 上设置了 3 个虚拟主机。 (见下面的配置)

每当我将浏览器定向到 stage.local 或 host.local 时,我都会被重定向到 dev.local,这是我首先设置的虚拟主机,但不应该是默认虚拟主机。

但是,当我转到 host.local/index.php 时,会显示正确的文件,不知何故它只是根目录搞砸了。 我不确定这怎么可能或如何解决。

配置似乎正确,这是完整文件:

#
# Use name-based virtual hosting.
#
NameVirtualHost *
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#


<VirtualHost *>
    ServerAdmin webmaster@host.local
    DocumentRoot "C:/xampp/htdocs/host.local/"
    ServerName host.local
</VirtualHost>

<VirtualHost *>
    ServerAdmin webmaster@stage.local
    DocumentRoot "C:/xampp/htdocs/stage.local/"
    ServerName stage.local
    ErrorLog "logs/stage.local-error.log"
    CustomLog "logs/stage.local-access.log" common
</VirtualHost>

<VirtualHost *>
    ServerAdmin webmaster@dev.local
    DocumentRoot "C:/xampp/htdocs/dev.local/"
    ServerName dev.local
    ErrorLog "logs/dev.local-error.log"
    CustomLog "logs/dev.local-access.log" common
</VirtualHost>

httpd -S 是这样说的:

*:*                    is a NameVirtualHost
     default server host.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:29)
     port * namevhost host.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:29)
     port * namevhost stage.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:35)
     port * namevhost dev.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:43)

(之后的一些 ssl 内容)

我的主机文件有以下条目:

127.0.0.1 localhost
127.0.0.1 host.local
127.0.0.1 stage.local
127.0.0.1 dev.local

【问题讨论】:

【参考方案1】:

对我来说它正在工作。您需要为例如定义端口:-

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#


<VirtualHost *:80>
    ServerAdmin webmaster@host.local
    DocumentRoot "C:/xampp/htdocs/host.local/"
    ServerName host.local
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@stage.local
    DocumentRoot "C:/xampp/htdocs/stage.local/"
    ServerName stage.local
    ErrorLog "logs/stage.local-error.log"
    CustomLog "logs/stage.local-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dev.local
    DocumentRoot "C:/xampp/htdocs/dev.local/"
    ServerName dev.local
    ErrorLog "logs/dev.local-error.log"
    CustomLog "logs/dev.local-access.log" common
</VirtualHost>

【讨论】:

以上是关于Apache 默认为一个虚拟主机,而不是其他虚拟主机的根文件夹的主要内容,如果未能解决你的问题,请参考以下文章

为 apache 添加虚拟主机

Apache 2.4 将本地虚拟主机重定向到 https 而不是 http

Apache虚拟目录

Wt WTableView 默认为分页而不是虚拟滚动

4.Apache和PHP结合 Apache默认虚拟主机

如何在 Apache 上设置 2 个虚拟主机,其中一个主机使用 SSL 运行而其他不使用?