Whether you are moving your domain to a new server or creating a new site, you may wish to test your site before the DNS entry is updated to show it live on the server.
You can override DNS settings locally by editing the hosts file on your computer. This allows you to view the website as if it were hosted at the server’s IP address, while everyone else who visits your site will still reach the IP address with the current nameservers.
When you are done testing the site you can edit your hosts file again to switch back to using DNS.
For Windows (Vista, 7, 8, 10)
You will need to first edit your hosts file, and then flush your DNS cache before you can view the site in your browser.
Editing the Hosts File
- In the Windows start menu, search for “Notepad”. Right click and select “Run as administrator”.
- Navigate to File > Open…
- Navigate to ‘c:\windows\system32\drivers\etc’ and change the display files type to “All Files”.
- Select the hosts file and click “Open”.
- For each IP address/site that you want to test, add a line at the bottom of the file with the IP address, the domain address, and the ‘www’ address separated by spaces. It should look something like this:
123.0.0.1 domainname.com www.domainname.com
- Alternatively, if you want to be able to compare the way the site looks on the new IP with the current one, you can leave off the ‘www.’ URL. This way, if you visit ‘domain.com’ you will see the new IP but if you visit ‘www.domain.com’ you will still see the domain with the current DNS. If you do this, the line should look something like this:
123.0.0.1 domainname.com
- Go to File > Save as.
- Change the save as type to “All Files”.
- Click on the hosts file in the window. If you click save without choosing the current hosts file from the window, Notepad will only save it was a ‘.txt’ file and not overwrite the actual hosts file.
- Click “Save”.
Flushing the DNS Cache
- In the Windows start menu, search for “cmd” and press enter. This will open Command Prompt.
- Type ‘ipconfig /flushdns’ and press enter. You will see the following
C:\Users\user>ipconfig /flushdns Windows IP Configuration Successfully flushed the DNS Resolver Cache. C:\Users\user>
Now you are ready to test the IP address by visiting your domain in a browser.
Restoring your Hosts File
When you are done testing the new IP, remember to restore the original hosts file.
- Open your hosts file in Notepad as before.
- Remove the line containing the IP address and domain you were testing.
- Go to File > Save As…
- Select “All Files” as the save as file type, and click on the hosts file.
- Click “Save”.
For Mac
You will need to first edit your hosts file, and then flush your DNS cache before you can view the site in your browser.
Editing the Hosts File
- Open Finder.
- Select “Applications” from the right sidebar.
- Find and open Utilities.
- Find and open Terminal.
- Type the following command and hit return:
sudo nano /etc/hosts
- Type your administrator password (it will not be visible for security purposes) and hit return.
- You should now see the nano text editor, which will display the file’s contents.
- For each site that you want to test, add a line to the file with the IP address and domain name separated by spaced. It should look like the following:
123.0.0.1 domainname.com www.domainname.com
- When you are finished adding the line(s), hit control + O to save, then control + X to exit.
Flush the DNS Cache
- Type the following into Terminal and hit return.
sudo killall -HUP mDNSResponder
- You may need to enter your administrator password again, and hit return.
You can now test the new IP by visiting your domain in a browser.
Restoring the Hosts File
When you are done testing IPs, remember to restore your hosts file to its original state.
- Follow the steps above (1-7) to open Terminal and run the nano text editor.
- Delete the lines that you added to the file.
- Hit control + O to save, and control + X to exit.