Migrating WordPress

Editing Hosts Files to Direct Domain Traffic

Understanding and using hosts files can be an invaluable tool when you’re moving WordPress sites or setting up development environments.

This is article 3 of 7 in a series on Migrating WordPress sites.

What are Hosts Files?

The hosts file is a local file on your computer that contains (or can contain) domain/IP address pairs that your computer uses to determine a site’s location on the Net.

Example Hosts File
Example Hosts File

Modifying Your Hosts File (telling geeky lies to your computer)

Since you can edit the hosts file, you can misdirect your computer into thinking that a site exists on a server that isn’t mapped in that domain’s public DNS record.  Or, you can make a computer or server think that a domain works that doesn’t even exist.

All you need are two things:

  1. Point the hosts file on your computer to the IP address of the server that will host the domain.
  2. Setup the server so it can serve up traffic for the domain.

Once a hosting server is setup to serve up traffic for a domain, it’ll fill any domain requests your computer sends to it.  The server will think and act like it is authoritatively in charge of the domain.

When to Change Your Hosts File

You can change your hosts file anytime you want to map a domain name to an IP address when public DNS servers can’t direct the traffic.  This might happen when:

  • The domain is not registered or valid.
  • You’re working with a domain on a server that is not referred to in public DNS records, such as a staging server, or development environment.

I tend to change my hosts file for three main reasons:

  1. Moving hosts, before DNS change,
  2. Access old hosting account after DNS change, and
  3. Setting up local development environments.

How to Change Your Hosts File

When you modify your computer’s hosts file, you’re basically tricking it by overriding the DNS for a specific domain. Your computer will only send requests to the IP address you’ve specified for the domain in the hosts file.

To do this, your hosts file needs to have two additional entries. One contains the IP address you want your machine to use and the other is the domain name itself.

For example, adding the two lines shown below will point both www.mysite.com and mysite.com to the IP address 65.187.312.189.

65.187.312.189 www.mysite.com
65.187.312.189 mysite.com

The following instructions explain how to locate and edit the hosts file on three major operating systems.

After you’ve made the desired domain changes and saved them, your machine will begin sending requests for that domain to the specified IP. After testing is complete, don’t forget to delete, or comment out, these entries in your hosts file to avoid confusion.

Changing Hosts File on Mac OS X 10.6 – Current

  1. Go to Applications > Utilities > Terminal
  2. Type sudo nano /private/etc/hosts in the Terminal window
  3. Enter your password
  4. Edit the Host File (NOTE: The hosts file contains comments (lines beginning with #) and several default hostname mappings. Add your desired mappings after the default ones.)
  5. After editing, save the Host File by pressing Control+x
  6. You must flush the DNS cache before your changes will take effect. Flush the cache by opening Terminal typing the following command:
 $ dscacheutil -flushcache

Changing Hosts File on Windows 8 and Windows 10

Both of these OS platforms use “User Account Control” (UAC), therefore you need to run Notepad as Administrator.

  1. Press the Windows key
  2. Type Notepad in the search field
  3. In the search results, right-click Notepad and select Run as administrator
  4. Select Continue when the “Windows needs your permission” window pops up
  5. After Notepad opens click File > Open
  6. Type C:\Windows\System32\Drivers\etc\hosts in the filename field
  7. Click Open
  8. Make your changes and save.

Changing Hosts File on Windows Vista and Windows 7

Both of these OS platforms use “User Account Control” (UAC), therefore you need to run Notepad as Administrator.

  1. Go to Start > All Programs > Accessories
  2. Locate Notepad. Right-click and select “Run as Administrator”
  3. Select Continue when the “Windows needs your permission” window pops up
  4. After Notepad opens click File > Open
  5. Type C:\\Windows\System32\Drivers\etc\hosts in the filename field
  6. Click Open
  7. Make your changes and save.

Changing Hosts File on Windows XP/2000/NT

  1. Go to Start > All Programs > Accessories > Notepad
  2. Select File > Open
  3. Type C:\\Windows\System32\Drivers\etc\hosts in the filename field
  4. Click Open
  5. Make your changes and save.

Changing Hosts File on Linux

  1. Open a terminal window
  2. Type sudo nano /etc/hosts
  3. Enter your login password
  4. Make your changes and save.

Testing Changes to Your Hosts File

If you’re unsure that the changes you made to your hosts file are working, you can open up a terminal or command line and ping the domain using:

ping mydomain.com

In return, you’ll get echos of the ping that return from the IP address associated with the domain you’re pinging.

So, if you changed your hosts file to point ivycat.com to 67.222.108.170, you should see something like this:

Erics-MacBook-Pro:~ eric$ ping ivycat.com
PING ivycat.com (67.222.108.170): 56 data bytes
64 bytes from 67.222.108.170: icmp_seq=0 ttl=50 time=46.744 ms
64 bytes from 67.222.108.170: icmp_seq=1 ttl=50 time=42.776 ms
64 bytes from 67.222.108.170: icmp_seq=2 ttl=50 time=42.634 ms
64 bytes from 67.222.108.170: icmp_seq=3 ttl=50 time=45.429 ms

Now that you have a basic understanding of how domains map to IP addresses, and how you can trick your computer into thinking domains exist in different places, let’s move on to Moving WordPress Sites using PHPMyAdmin & FTP.