How to Edit and Customise the Hosts File in Windows 11 for Local Web Development and Domain Redirection

When developing web applications, migrating production websites to new server infrastructure, or testing local virtual hosts, waiting for public Domain Name System (DNS) propagation can delay workflows. Even before your operating system queries external DNS resolvers or router servers, it consults a local system configuration file known as the hosts file. By mapping domain names directly to custom IP addresses locally, you can preview websites, test development staging domains, or redirect network requests without altering global DNS records.

Because the hosts file in Windows 11 resides in a protected system directory, modifying it requires elevated administrator privileges and proper text encoding to prevent file-locking errors. Additionally, Windows 11 supports the modern Microsoft PowerToys Hosts File Editor for a clean graphical experience. This comprehensive guide covers how to locate, edit, and customise the Windows 11 hosts file using Notepad, PowerShell, and PowerToys, alongside techniques to flush DNS caches and troubleshoot antivirus file-lock errors.

System Path: The hosts file in Windows 11 is located at: C:\Windows\System32\drivers\etc\hosts. It does not have a .txt or any other file extension.

How the Windows Hosts File Operates

The hosts file is a plain-text configuration file inherited from early ARPANET host tables. Its operation follows a straightforward lookup hierarchy:

  • Local Priority: When you enter a web address (such as dev.myproject.local or example.com) into a browser or development tool, Windows 11 checks the hosts file first. If an entry exists for that hostname, Windows routes the connection directly to the specified IP address, completely bypassing external DNS lookups.
  • Formatting Rules: Each entry occupies a single line consisting of the target IP address, followed by at least one space or tab, followed by the domain name (for example, 127.0.0.1 staging.mysite.com).
  • Comments: Any line starting with a hash symbol (#) is treated as an ignored comment, useful for documenting entries or temporarily disabling redirects.

Step 1: Editing the Hosts File Using Notepad with Administrator Rights

Because the drivers\etc\ directory requires administrative permissions to write files, you must launch your text editor with elevated rights:

  1. Click the Start button or press Win on your keyboard.
  2. Type Notepad into the search field.
  3. Right-click the Notepad application icon and select Run as administrator. Confirm the User Account Control (UAC) prompt by clicking Yes.
  4. In Notepad, click File > Open… (or press Ctrl + O).
  5. In the file dialogue address bar, navigate to:
    C:\Windows\System32\drivers\etc
  6. In the bottom-right corner of the file dialogue, change the file type filter from Text Documents (*.txt) to All Files (*.*).
  7. Select the file named hosts and click Open.

Step 2: Adding Custom Domain Mappings

Scroll to the bottom of the hosts file and add your custom redirection lines. Here are typical web development and testing examples:

# Local development virtual hosts
127.0.0.1       mysite.local
127.0.0.1       api.mysite.local

# Staging server pre-migration preview
192.168.1.150   staging.clientproject.co.uk

# Point production domain to new server IP for pre-launch testing
203.0.113.45    clientproject.co.uk
203.0.113.45    www.clientproject.co.uk

# Local IPv6 loopback mapping
::1             mysite.local

Once you have finished adding your entries, click File > Save (or press Ctrl + S). Ensure Notepad does not append a .txt extension to the filename.

Step 3: Flushing the Windows DNS Resolver Cache

After saving your hosts file edits, Windows may still have previous DNS query results stored in its memory cache. To force Windows 11 to recognise your new mappings immediately:

  1. Press Win + X and select Terminal (Admin) or PowerShell (Admin).
  2. Enter the following command and press Enter:
    ipconfig /flushdns
  3. The console will confirm: “Successfully flushed the DNS Resolver Cache.”
  4. Test your mapping using ping:
    ping mysite.local
  5. Verify that the response returns the custom IP address you specified in your hosts file.

Step 4: Using Microsoft PowerToys Hosts File Editor (GUI Method)

If you prefer an intuitive graphical interface that allows you to toggle entries on and off with checkboxes without opening text files:

  1. Install Microsoft PowerToys from the Microsoft Store or via winget:
    winget install Microsoft.PowerToys
  2. Open PowerToys from your Start Menu.
  3. Click Hosts File Editor in the left sidebar and toggle Enable Hosts File Editor to On.
  4. Click Launch Hosts File Editor.
  5. Click New entry. Enter the IP address, domain name, and an optional comment, then click Add.
  6. You can temporarily disable any host mapping at any time by toggling its individual switch without deleting the line.

Troubleshooting Common Hosts File Issues in Windows 11

Issue Encountered Primary Cause Resolution Procedure
“Access Denied” when saving in Notepad Notepad opened without administrator privileges Close Notepad, right-click its Start Menu icon, select Run as administrator, and reopen the file.
File saved as hosts.txt Notepad defaulted to .txt extension filter Open File Explorer, check File name extensions under View, and rename the file to remove .txt.
Browser ignores hosts file mappings Secure DNS (DNS over HTTPS) enabled in browser In Chrome or Edge, go to Settings > Privacy > Security, and disable Use secure DNS temporarily.
Antivirus reverts hosts file edits Third-party security software protecting hosts integrity Open your antivirus settings and whitelist or add an exclusion for the hosts file.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.