If you need to check, test or access a new website that is only accessible on a temporary URL, you may need to edit the hosts file to properly test the website. This method of access can also be used to make changes to a staging website prior to the website going live, or to simply verify the outcome of a migration.
It is commonly required to use the hosts file when working with Content Management Systems such as WordPress or Joomla that are installed to respond to the domain name instead of the IP address.
The hosts file itself need to be updated with specific syntax, this will be in the format of ‘ip domain(s)’, for example.
127.0.0.1 mywebsite.com www.mywebsite.com
Once this is set you can access the website on the new server using its normal domain name/URL. The location of the hosts file and how to edit this depends on the operating system you are using.
Below are guides for Windows, Mac and Linux.
Windows
- Locate ‘notepad‘ in the start menu
- Right click on notepad and select ‘Run as Administrator‘
- Click ‘File‘ and then ‘Open‘
- Navigate to the following location ‘C:\Windows\System32\drivers\etc‘
- Change ‘Text Documents (*.txt)‘ to ‘All Files‘
- Select ‘hosts‘ and click ‘Open‘
- Type in the details as required
- When done save the file by clicking on ‘File‘ and then ‘Save‘
Mac
- Open Finder
- Select ‘Applications‘, and then ‘Utilities‘You can also hold down the ‘Control‘ and ‘Shift‘ keys and press ‘U‘
- Open ‘terminal‘
- Type in ‘sudo nano /private/etc/hosts‘
(If this file is blank use ‘sudo nano /etc/hosts‘) - Type in your computer’s password and press ‘Enter‘
The screen will remain blank while doing this - Use the ‘down arrow‘ key to navigate to the end of the file
- Type in the details as required
- Hold down the ‘Control‘ key and press ‘O‘ to save, then press ‘Enter‘
- Hold down the ‘Control‘ key and press ‘X‘ to exit nano
- You can now exit terminal