Local DNS Settings: Map a Domain to a Local IP Address

On occasion you may have reason to map a domain name to a specific IP address on an individual computer.  I find this most useful when developing locally so that I can use a website’s actual domain name but map it to the local dev version of the site.

Using a custom domain mapping is particularly important when developing with WordPress because the siteURL plays such a central role in creating links and resources in the WP structure. If you can use the actual domain during development, you’ll save yourself trouble later having to update the siteURL and any data that would have referenced the in-development domain.

Windows Hosts file

Local DNS settings allow you to manage custom domain mappings to IP addresses of your choosing. These may be internal IPs within your local area network, or public IPs outside your network. These mappings are set up by editing the “Hosts” file on Windows or Mac systems. Here’s how:

Edit the Hosts File in Windows

1. Open the Hosts file in Notebook or some other simple text editor editor. Where is the hosts file you ask? Depends on your setup, but for most Windows systems the directory path should be something like

\\Windows\System32\drivers\etc\hosts

Depending on your security settings, you may need to open your text editor in administrator mode (‘run as administrator’) in order to save your edits.

2. At the top of the file there are instructions for how to use the file, all preceeded by the # comment tags.

3. Below this are the actual mappings. The first column contains the preferred IP destination, while the second column contains the domain to be mapped. The domains in the second column will resolve to the corresponding IPs in the first column. A few important points:

  • Naked domains and subdomains (i.e., www.) must be mapped separately.
  • You must use an IP address in the first column, not another domain name.
  • Use hashes to disable mappings temporarily

4. Once you’ve made your settings, save the file.

5. Open the Command Prompt and update your system’s DNS cache with the command:

ipconfig /flushdns

6. All browsers should now follow these mappings for the domains listed in this Hosts file.

Edit the Hosts File on a Mac OSX

1. Open Terminal and open the hosts file using the following command:

sudo nano /private/etc/hosts 

2. You may be asked to enter your administrator password. Go ahead and do that to continue.

3. The Hosts file will open. Much like the Windows version, there are instruction comments at the top, followed by the mappings. The first column contains the preferred IP destination, while the second column contains the domain to be mapped. The domains in the second column will resolve to the corresponding IPs in the first column.

4. Add your new mappings. Just as with the Windows version, keep in mind that:

  • Naked domains and subdomains (i.e., www.) must be mapped separately.
  • You must use an IP address in the first column, not another domain name.
  • Use hashes to disable mappings temporarily

5. When finished save your changes by pressing Control-o, then hit return to confirm the filename, then Control-x to close the Hosts file.

6. Lastly, update your DNS cache from Terminal using the command:

dscacheutil -flushcache

7. All browsers should now follow these mappings for the domains listed in this Hosts file.

 

Leave a Reply

You must be logged in to post a comment.