I very rarely do this stuff, but because there is a programmer lurking inside me. I didn’t want to forget this, so basically it’s a note for myself 😃

How to edit local Hosts File for testing your website:

This comes in handy when you migrate from your website to a new server, and you want to double-check everything is fine before pointing your DNS (Domain Name Servers) to the new server’s IP address.

This “HOST” file can be used to force your computer to resolve your website to a specific IP address rather than resolving your website using the DNS found in the name servers.

Anyways, on to the fun…

Goto Terminal

Open up “Terminal” a command line interface (aka CLI, back from the old days). You’ll find it in your Mac, just search your Application / Utilities folder, or search via spotlight or Alfred launcher if you use that.

It will look something like this…

Crucial Commands

Type the following (or cut n paste from below):

sudo nano /etc/hosts

You’ll be prompted to enter your Mac user’s password, if the cursor doesn’t blink – don’t worry, just hit Enter/Return key.

Add the following below:

SERVER_IP_ADDRESS yourdomain.com
SERVER_IP_ADDRESS www.yourdomain.com

Replace SERVER_IP_ADDRESS with your new server’s IP address. Then replace “yourdomain” with of course your domain! Best to use both versions, incase you use www or go straight to the domain. And remember to have a SPACE after the IP address, otherwise it won’t work!

Closing Commands

Hit the following on your keyboard: control+o , then enter/return key, followed by control+x – remember those are keys on Mac, you don’t type them out!

Remember To Flush

And then flush your cache with these commands:

dscacheutil -flushcache

Hit enter/return key and then the following for good measure!

sudo dscacheutil -flushcache

Happy Website

That’s it. Now you can browse your site! After you finished testing, go ahead and point your DNS to the SERVER_IP_ADDRESS, and remember to remove the above block (in your host file) and repeat the closing commands.

Also check out: How to Edit the Hosts File on macOS (Mac OS X) if my explanation wasn’t good enough.