如何在 windows wamp 服务器中使用相同的 ip 指向两个网站

How to point two website in windows wamp server with same ip

我有两个网站 www.test1.comwww.test2.com。我在 windows wampp server 中托管了这两个网站,其中 IP 名为 xx.xxx.xx.xx。但是在打开两个站点时 www.test1.com 是 comming.Here www.test1.com 文件在“test1”文件夹中,www.test2.com 在“test2”文件夹中。 我在 wampp 中尝试了虚拟主机...但它不起作用...

这是我的 \wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhosts.conf 文件

<VirtualHost *:80>
     DocumentRoot "c:/wamp/www/test1"
     ServerName test1
     ServerAlias test1.com
 </VirtualHost>

<VirtualHost *:80>
     DocumentRoot "c:/wamp/www/test2"
     ServerName localhost
     ServerAlias test2.com
     <Directory  "c:/wamp/www/test2">
        AllowOverride All
    Require local
    Require all granted
    Allow from all
     </Directory>
 </VirtualHost>

这是我的 C:\Windows\System32\drivers\etc\hosts 文件

127.0.0.1      localhost
127.0.0.1      test1.com
127.0.0.1      test2.com

第一行需要

NameVirtualHost *:80

您的两个域名都将转到 httpd-vhost.conf 文件中首先定义的站点,因为当 Apache 找不到主机定义中请求的站点时,这是默认行为。这通常意味着您在定义 VHOSTS 时做错了。

您的主机定义有点错误,并且您混合使用 Apache 2.2 和 2.4 安全语法,这经常会导致问题,请尝试这些

# Should be the first VHOST definition so that it is the default virtual host
# Also access rights should remain restricted to the local PC and the local network
# So that any random ip address attack will recieve an error code and not gain access
<VirtualHost *:80>
    DocumentRoot "C:/wamp/www"
    ServerName localhost
    <Directory  "C:/wamp/www">
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

<VirtualHost *:80>
     DocumentRoot "c:/wamp/www/test1"
     ServerName test1.com
     ServerAlias www.test1.com
     <Directory  "c:/wamp/www/test1">
        AllowOverride All
        Require local
        Require all granted
     </Directory>
 </VirtualHost>

<VirtualHost *:80>
     DocumentRoot "c:/wamp/www/test2"
     ServerName test2.com
     ServerAlias www.test2.com
     <Directory  "c:/wamp/www/test2">
        AllowOverride All
        Require local
        Require all granted
     </Directory>
 </VirtualHost>

现在,为了您可以在 WAMPServer PC 上本地查看站点,hosts 文件应该如下所示。请记住,这些条目只会影响包含 HOSTS 文件的 PC,而不会影响互联网访问或远程用户使用这些域名的能力。

# IPV4 loopback
127.0.0.1      localhost
127.0.0.1      test1.com
127.0.0.1      test2.com
# IPV6 loopback
::1      localhost
::1      test1.com
::1      test2.com