To locally point a domain using a hosts file makes it very easy to test a website you’ve built on another server without affecting general access to the current website.
If the website you’re building is using a CMS such as WordPress or Magento, you will not have to go through the hassle of changing the URL the site is using. If you on the other hand use a temporary domain, you’ll need to update all URLs on the site once you’re ready to go live.
Below, we describe how you point a domain locally in the most commonly used operating systems.
Windows 7, 8, and 10
- Open Notepad as administrator: In Windows 8 and 10, press the
Windows
key on your keyboard and start typingNotepad
to find the program. In Windows 7, use the search field under the Start menu to find it. Either way, right-click the program and clickRun as Administrator
. - Open the hosts file in the program via
File
->Open
. In the filename field, enterC:\Windows\System32\drivers\etc\hosts
. - You will now see number of comment lines (starting with #). Add an IP address and the domain you want to point at the bottom of the file. If the domain is to be pointed towards a web hosting account on our servers, you can find the correct IP address via these instructions. Below follows examples of such rows:
91.201.63.148 exempel.se 91.201.63.148 www.exempel.se
- Don’t forget to save the hosts file ones you’re done editing it! If the program tells you you cannot save, make sure that you started Notepad as administrator.
MacOS 10.6+ and Linux/Unix
- Begin by opening a terminal:
- On macOS, press
cmd
+space
on your keyboard and typeterminal
, and pressEnter
. You can also find the appTerminal
via theUtilities
folder in yourApplications
folder, if you rather click your way to the app. - If you’re using Linux/Unix, start your favourite terminal. Which ones you have access to vary depending on your distribution.
- On macOS, press
- Open the hosts file with a text editor as the root user, e.g. with the command
sudo nano /etc/hosts
. Keep in mind that your user must have sudo permissions on Linux/Unix for this to work. If you run macOS, your user needs to be administrator. - Add the IP address and the domain on a new line at the end of the file (in the same way as displayed in the Windows example above), and save it. If you use nano, you can save by pressing
ctrl
+X
on your keyboard and confirm that you want to save with theY
key.