1、修改 apache 的配置文件 httpd.conf
首先我们打开 apache 的配置文件 httpd.conf,比如我的是在 F:\wamp\bin\apache\Apache2.2.17\conf
文件夹下,找到这一行:
#Include conf/extra/httpd-vhosts.conf 把注释去掉(删掉最前面的井号)。
2、修改 httpd-vhosts.conf 文件
打开 F:\wamp\bin\apache\Apache2.2.17\conf\extra
文件夹下的 httpd-vhosts.conf 文件,加上这么一段,比如:
<VirtualHost *:80>
DocumentRoot "F:\wamp\www\google"
ServerName www.google.com
ServerAlias google.com
</VirtualHost>
3、修改 hosts 文件
打开 C:\WINDOWS\system32\drivers\etc\hosts
文件,添加一句:
127.0.0.1 www.google.com
4、新建 google 文件夹
在 www 文件夹下新建一个名为 google 的文件夹,在里面添加一个任意的文件,比如 index.html,这时就可以用 www.google.com/index.html 访问到。
5、再次修改 httpd-vhosts.conf 文件
这时如果去访问 localhost,发现又访问不了了,需要再次修改 httpd-vhosts.conf 文件,添加:
<VirtualHost *:80>
DocumentRoot "F:\wamp\www"
ServerName localhost
</VirtualHost>
这时访问 localhost/index.html,就 ok 了!