作为站长或者web开发人员要经常进行web程序的本地测试,一般可以127.0.0.1来访问本地web,但是这样也造成了一些不便,修改完的程序上传到服务器之前有时还得针对真实域名进行修改,才能在服务器上正确运行。本地测试也用服务器域名来进行不是更好?这样本地弄完不许要任何改动直接就可以在服务器上运行。这时候我们可以通过修改本地hosts文件的方法来进行。

hosts文件路径:C:\WINDOWS\system32\drivers\etc,用记事本打开,可以看到内容如下:

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
127.0.0.1 activate.adobe.com
127.0.0.1 practivate.adobe.com
127.0.0.1 ereg.adobe.com
127.0.0.1 activate.wip3.adobe.com
127.0.0.1 wip3.adobe.com
127.0.0.1 3dns-3.adobe.com
127.0.0.1 3dns-2.adobe.com
127.0.0.1 adobe-dns.adobe.com
127.0.0.1 adobe-dns-2.adobe.com
127.0.0.1 adobe-dns-3.adobe.com
127.0.0.1 ereg.wip3.adobe.com
127.0.0.1 activate-sea.adobe.com
127.0.0.1 wwis-dubc1-vip60.adobe.com
127.0.0.1 activate-sjc0.adobe.com

在文件后面添加“127.0.0.1 你的域名”,就可以了。比如我的博客域名www.yizu.org,那就添加:“127.0.0.1 www.yizu.org”,注意IP与域名之间有个空格。然后保存即可,这样就可以用域名访问本地web了,但是用完了记得删除加的那一行,否则你就不能正确访问你的服务器上网站了。