127.0.0.1 is accessible/working but localhost not accessible/ not working
Categories:
Troubleshooting 'localhost' Not Working While '127.0.0.1' Is Accessible

Discover why your 'localhost' might be failing even when '127.0.0.1' works, and learn how to resolve common issues in Windows, Apache, PHP, and phpMyAdmin environments.
It's a common and often frustrating scenario for developers: you're trying to access your local web server, perhaps for a PHP project or phpMyAdmin, and typing localhost
into your browser yields nothing, yet 127.0.0.1
works perfectly. This indicates that your web server (like Apache) is running correctly and listening on the standard loopback address, but there's a problem with how your system resolves the hostname localhost
to that address. This article will guide you through the typical causes and solutions for this issue, focusing on Windows environments with Apache, PHP, and phpMyAdmin setups.
Understanding the Loopback Address and Hostname Resolution
Before diving into solutions, it's crucial to understand the difference between localhost
and 127.0.0.1
. Both refer to the 'loopback' interface, which is a virtual network interface that allows your computer to communicate with itself. 127.0.0.1
is the standard IPv4 address assigned to this interface. localhost
, on the other hand, is a hostname that, by default, should resolve to 127.0.0.1
(and ::1
for IPv6). The process of translating hostnames to IP addresses is handled by your system's DNS resolver, which first checks the local hosts
file before querying external DNS servers.
flowchart TD A[User types 'localhost'] --> B{DNS Resolver} B --> C{"Check hosts file (C:\Windows\System32\drivers\etc\hosts)"} C -->|Entry found for 'localhost'| D[Resolve to 127.0.0.1] C -->|No entry / Incorrect entry| E[Query DNS Servers] E --> F{External DNS Server} F -->|No resolution for 'localhost'| G[Connection Fails] D --> H[Connect to Web Server (Apache)] H --> I[Access PHP/phpMyAdmin] G --> J[Error: 'localhost' not found] K[User types '127.0.0.1'] --> H
Hostname Resolution Flow for 'localhost' vs. '127.0.0.1'
Common Causes and Solutions
The primary reason localhost
fails while 127.0.0.1
works is almost always related to an incorrect or missing entry in your system's hosts
file, or sometimes a misconfigured proxy or firewall. Let's explore the most common culprits and how to fix them.
hosts
file. Otherwise, you won't be able to save your changes.1. Check and Edit Your Hosts File
The hosts
file is a critical component for local hostname resolution. On Windows, it's located at C:\Windows\System32\drivers\etc\hosts
. Open this file with a text editor (like Notepad) run as administrator. Look for lines containing localhost
or 127.0.0.1
. Ensure you have the following entries, uncommented:
2. Verify Apache Configuration
While less common for this specific issue, ensure Apache is configured to listen on 127.0.0.1
or all interfaces. Check your httpd.conf
file (e.g., C:\wamp\bin\apache\apache2.4.x\conf\httpd.conf
for WAMP). Look for the Listen
directive. It should typically be Listen 0.0.0.0:80
or Listen 80
(which defaults to all interfaces), or specifically Listen 127.0.0.1:80
. If it's only listening on a specific external IP, localhost
might not work.
3. Check for Proxy Settings
Browser or system-wide proxy settings can sometimes interfere with localhost
resolution. Ensure that no proxy is configured to bypass localhost
or that it's correctly configured to handle local addresses. In Chrome, you can check this under Settings -> System -> Open your computer's proxy settings.
4. Temporarily Disable Firewall/Antivirus
Although unlikely to differentiate between localhost
and 127.0.0.1
, a strict firewall or antivirus program could potentially block connections. As a diagnostic step, try temporarily disabling them to see if the issue resolves. Remember to re-enable them afterwards.
5. Flush DNS Cache
After making changes to the hosts
file or network settings, it's a good practice to flush your DNS resolver cache to ensure the system picks up the new configuration immediately. Open Command Prompt as administrator and run the following command:
127.0.0.1 localhost
::1 localhost
Essential entries for your hosts
file
ipconfig /flushdns
Command to flush DNS resolver cache
Specific Considerations for WAMP/XAMPP and phpMyAdmin
If you're using WAMP or XAMPP, these packages typically configure Apache and PHP automatically. However, issues can still arise. For phpMyAdmin, ensure its configuration (config.inc.php
or config.sample.inc.php
) correctly points to 127.0.0.1
as the host for MySQL, which it usually does by default. The problem is almost always at the OS level (hosts file) rather than the application level for this specific localhost
vs 127.0.0.1
discrepancy.
hosts
. Always make a backup copy before making changes. Incorrect modifications can lead to network connectivity issues.