When migrating a website to a new server, one of the biggest challenges is testing the site thoroughly before making it live. You want to ensure everything works perfectly on the new server without affecting your visitors’ experience on the current live site. This is where your computer’s hosts file becomes an invaluable tool.
TL;DR: Test a Migrated Website with Your Hosts File
- Find the Hosts File
Windows:C:\Windows\System32\drivers\etc\hosts
macOS/Linux:/etc/hosts - Edit with Admin Rights
Windows: Open Notepad as Administrator
macOS/Linux:sudo nano /etc/hosts - Add Your Entry
192.168.1.100 yourdomain.com
192.168.1.100 www.yourdomain.com - Flush DNS Cache
Windows:ipconfig /flushdns
macOS:sudo dscacheutil -flushcache
Linux:sudo systemctl restart systemd-resolved - Test in Browser → Use incognito/private mode
- Revert After Testing → Remove entries, save, flush DNS, update registrar DNS
What is a Hosts File?
The hosts file is a simple text file that maps hostnames to IP addresses on your local computer. It acts like a personal phone book for your computer, telling it where to find specific websites. When you type a domain name into your browser, your computer first checks the hosts file before querying DNS servers. This means you can override DNS settings locally and direct specific domains to different IP addresses – perfect for testing purposes.
Why Use Hosts File for Site Migration Testing?
Using your hosts file for migration testing offers several key advantages:
- Safe Testing Environment: Test your migrated site without affecting live traffic or search engines
- Comprehensive Testing: Verify all functionality, forms, databases, and third-party integrations
- No DNS Propagation Delays: Instant access to test the new server without waiting for DNS changes
- Risk-Free Process: Make mistakes and fixes without impacting your live audience
- Team Collaboration: Multiple team members can test simultaneously using the same method
Locating Your Hosts File
The hosts file location varies by operating system:
Windows
- Path:
C:\Windows\System32\drivers\etc\hosts - Access: Run Notepad as Administrator, then open the file
macOS
- Path:
/etc/hosts - Access: Use Terminal with sudo privileges
Linux
- Path:
/etc/hosts - Access: Use your preferred text editor with sudo privileges
Step-by-Step Instructions
For Windows Users
- Open Notepad as Administrator
- Press Windows key + R
- Type
notepadand press Ctrl + Shift + Enter - Click “Yes” when prompted for administrator access
- Open the Hosts File
- In Notepad, click File → Open
- Navigate to
C:\Windows\System32\drivers\etc\ - Change file type to “All Files (.)”
- Select the
hostsfile and click Open
- Add Your Entry
- Scroll to the bottom of the file
- Add a new line with this format:
192.168.1.100 yourdomain.com 192.168.1.100 www.yourdomain.com- Replace
192.168.1.100with your new server’s IP address - Replace
yourdomain.comwith your actual domain
- Save the File
- Press Ctrl + S to save
- Close Notepad
For macOS Users
- Open Terminal
- Press Cmd + Space, type “Terminal”, and press Enter
- Edit the Hosts File
- Type:
sudo nano /etc/hosts - Enter your admin password when prompted
- Type:
- Add Your Entry
- Use arrow keys to navigate to the bottom
- Add a new line:
192.168.1.100 yourdomain.com 192.168.1.100 www.yourdomain.com - Save and Exit
- Press Ctrl + O to save
- Press Enter to confirm
- Press Ctrl + X to exit
For Linux Users
- Open Terminal
- Use your system’s method to open a terminal
- Edit the Hosts File
- Type:
sudo nano /etc/hosts(or use vim/gedit) - Enter your password when prompted
- Type:
- Add Your Entry
- Navigate to the bottom of the file
- Add your entry using the same format as above
- Save and Exit
- In nano: Ctrl + O, Enter, then Ctrl + X
- In vim: Press Esc, type
:wq, press Enter
Testing Your Configuration
After editing your hosts file, follow these steps to verify it’s working:
- Flush DNS Cache
- Windows: Open Command Prompt as Administrator and run
ipconfig /flushdns - macOS: Run
sudo dscacheutil -flushcache - Linux: Run
sudo systemctl restart systemd-resolvedorsudo service nscd restart
- Windows: Open Command Prompt as Administrator and run
- Test in Browser
- Open a new browser window or incognito/private mode
- Navigate to your domain
- You should now see the site from your new server
- Verify the IP Address
- Open Command Prompt or Terminal
- Type:
ping yourdomain.com - Confirm it shows your new server’s IP address
Important Considerations
Browser Caching
Clear your browser cache or use private/incognito mode to ensure you’re seeing fresh content from the new server rather than cached versions from the old site.
SSL Certificates
If your site uses HTTPS, ensure SSL certificates are properly configured on the new server. You may see security warnings if certificates don’t match the domain.
Database Connections
Verify that all database connections, contact forms, e-commerce functionality, and third-party integrations work correctly on the new server.
Mobile Testing
Remember that hosts file changes only affect the computer where you made them. Test mobile responsiveness using browser developer tools or by setting up hosts files on mobile devices if needed.
Troubleshooting Common Issues
Changes Not Taking Effect
- Ensure you saved the hosts file properly
- Flush your DNS cache
- Restart your browser
- Check for typos in IP addresses or domain names
Permission Denied Errors
- Make sure you’re running your text editor as Administrator (Windows) or using sudo (macOS/Linux)
- Verify the hosts file isn’t set to read-only
Website Still Shows Old Version
- Clear browser cache and cookies
- Try a different browser or incognito mode
- Double-check your hosts file syntax
Reverting Your Changes
Once you’re satisfied with your testing and ready to make the site live:
- Remove Hosts File Entries
- Open your hosts file using the same method as before
- Delete the lines you added for testing
- Save the file
- Flush DNS Cache Again
- Use the same commands mentioned in the testing section
- Update Your DNS
- Log into your domain registrar or DNS provider
- Update the A records to point to your new server’s IP address
- Wait for DNS propagation (typically 24-48 hours)
Best Practices
- Always backup your original hosts file before making changes
- Document which entries you’ve added for testing
- Use comments in your hosts file (lines starting with #) to label your entries
- Remove test entries promptly after migration completion
- Never leave test entries in production environments
Conclusion
Using your computer’s hosts file is an essential technique for safely testing website migrations. It provides a risk-free environment to thoroughly validate your new server setup before directing live traffic to it. By following these steps, you can ensure a smooth migration process and minimize downtime for your website visitors.
Remember, the hosts file is a powerful tool that should be used responsibly. Always document your changes and revert them once your testing is complete. With proper use of the hosts file, you can migrate websites with confidence, knowing that everything has been thoroughly tested before going live.