I ran into the problem, that localhost redirects to a specific domain instead of 127.0.0.1.
My configuration is a local apache/mysql/php installation on MacOS Mojave using vhosts for different projects that are configured in hosts to redirect properly.
When I enter 127.0.0.1 in any browser, I get to my overview page lying in the www root. The same should happen, when I enter localhost in the browser, but instead it redirects to a domain of a project I was working on in the past. It happens also without network connection, so the configuration is on this machine.
My /etc/hosts file:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost project1.local project2.local
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
The first line seems to be ignored and localhost doesn't resolve to 127.0.0.1.
I only have two vhost configurations plus default configuration with no wrong redirection and also the httpd.conf lookes fine. I searched the whole machine with grep -lr "redirectiondomain.com" * without results. I tried with apache switched off with the same result and looked up the MacOS network settings but I can't find anything related to the wrong redirection neither any other solution on stackoverflow or google that solves my issue. Has anybody a clue how the wrong redirection of localhost could be set somewhere else?
Edit: My hostname in MacOS is set. I tried localhost without port, with :80 and :8080
Whenever I start Tomcat, and open http://localhost:8080 I get the following error.
Safari can’t open the page “localhost:8080” because Safari can’t connect to the server “localhost”.
It used to work before today. All I did earlier today was block some websites using the host files, but I restored them back again, and now it looks like this.
#
##
# Host Database
#
#
# localhost is used to configure the lookback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
If I start and stop the server using Eclipse, it works. But localhost can’t connect to server using Safari or Google Chrome either..
I know there are a lot of similar questions, but none of them seemed to help.
Thanks a lot in advance!
I am running a RoR app on my local. I have some translations in my hosts file.
127.0.0.1 localhost
127.0.0.1 example.dev
127.0.0.1 www.example.dev
127.0.0.1 api.example.dev
They were working fine, until I updated my OS to 10.11.4. If I send ping request to www.example.dev, it seems to work fine. The IP it translates to is 127.0.0.1.
But when I visit www.example.dev in my web browser, it seems to ignore hosts file and ping DNS server for IP.
I am not sure how, but going to localhost in browser does translate to 127.0.0.1.
Dont know why but ive disabled my lookback adapter and ipv6 and now its working.
so in your host file disable these two items
# ::1 localhost
# fe80::1%lo0 localhost
Some fubar osx bug... I miss steve....
I came across this while searching for an answer to a similar problem and wanted to post my findings for anyone else in the same position.
As a team of three, we found that edits to my /etc/hosts file appeared to work, while editing the other two's host files seemingly did not. Upon further digging, we realized I was on OS X 10.10, while they were on newer versions.
We found, after trying about a million things, that additions to the hosts file in 10.11 and up apparently could not have more than one space between the IP and the domain, for example:
DID NOT WORK:
1.2.3.4 some.site.com
DID WORK:
1.2.3.4 some.site.com
After making this change, we immediately started seeing expected results without any cache clears, reboots, or otherwise.
The host you are overriding is resolving via IPV6. ::1 is the loopback address for IPV6 which is analogous to 127.0.0.1 in IPV4. Add the following entries as the last lines in your /etc/hosts file.
::1 localhost::1 www.example.dev
I've been facing this problem in mountain lion 10.8.3 and I cannot seem to find an answer. The problem is that I can access my "localhost" using username.local or http://127.0.0.1/ but not through localhost. I'm using apache 2.2 and virtualhostx, and for now is fine becuase I map every virtual host with a folder, but when developing in java, using Tomcat, I cannot access localhost, also there I have to access username.local..
my hosts file
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
If I cant find a solution I only can think of a fresh reinstall.. And this will be very annoying..
check your httpd.conf for this line
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
i had the same issue. Then i discovered that the httod-vhosts.conf file was commented out when i upgraded my OS.
Since setting up my development environments on Mac OS X Lion (brand new macbook air purchased in January 2012), I have noticed that resolving to a virtual host is very slow (around 3 seconds) the first time but after that is fast as long as I continue loading it regularly.
If I leave it untouched for a couple of minutes and then reload again, the first reload is (again) painfully slow; seems like something is being cached.
As can be seen below I am not using the .local TLD.
My setup: Apache 2 - MySQL - PHP installed and enabled - added a couple of virtual hosts one of which I created for localhost
My /etc/hosts:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 myproject.dev
::1 myproject.dev
fe80::1%lo0 myproject.dev
My virtual host set-up in username.conf:
NameVirtualHost *:80
<Directory "/Users/myusername/Sites/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Users/myusername/Dropbox/dev_envs/
</VirtualHost>
<VirtualHost *:80>
ServerName myproject.dev
DocumentRoot /Users/myusername/Dropbox/dev_envs/myprojectname
</VirtualHost>
I had the exact same problem and it was driving me crazy!
Put all your hosts file entries for localhost into one line like so:
127.0.0.1 localhost myproject.dev myotherproject.dev
::1 localhost
fe80::1%lo0 localhost
Worked like a charm for me. Seems like a bug in Lion.
There's another issue 10.7.* to 10.8.4 for sites ending in .local which causes five second lookups. Details and solution courtesy Bram Van Damme’s blog post found here.
“By default, any hostname ending in .local is treated as a Bonjour host rather than by querying the DNS server entries in Network preferences.”
“To fix this problem (without having to rename each vhost) you need to add IPv6 entries for each of your vhosts in your /etc/hosts file:”
::1 mysite.local
fe80::1%lo0 mysite.local
127.0.0.1 mysite.local
I had the same problem, also on Lion.
Strangely, my solution was the opposite of Jeremy's. I had a whole bunch of someproject.dev entries on one line in /etc/hosts. Loading a site on any of them the first time took forever, like a minute or so. If I used it again within 5 seconds or so it was very fast, but much longer and it would again take a minute. I had suspected all sorts of things, mysql connections, Ruby versions, Rails bugs, Apache, Phusion Passenger. Until I finally looked at the Console and realized that DNS lookups were being attempted.
So, I put all of them on seperate lines:
127.0.0.1 localhost
127.0.0.1 myproject.dev
127.0.0.1 myotherproject.dev
And suddenly everything was snappy again. Same on both my machines.
Specifying same host for IPv6 ::1 helped me.
127.0.0.1 something.local.mydomain.org
::1 something.local.mydomain.org
Make sure to put the IP v6 entries not in the line with localhost
::1 localhost
the IP v6 entries go in a separate line
fe80::1%lo0 here and_here
It is sometimes really fast now, but there are rare exceptions where the old lags come back. They might however be based on other reasons.
On OSX El Capitan what worked for me was making a duplicate IPv6 entry right above the IPv4 entry like so
fe80::1%lo0 demo.test.dev
127.0.0.1 demo.test.dev
Ensuring that the host names are defined at the beginning of the file made the difference for me.
By default the line 127.0.0.1 localhost is already at the beginning, just add your entries on the same line.
I had the same problem and found it to be caused by enabling IPv6 on my LAN, but not having IPv6 configured correctly between my network and my ISP. Apparently the IPv6 DNS-server takes precedence over IPv4 DNS when the client is given both. It took a couple of seconds (on every attempt) for the client to find that the IPv6 DNS was unreachable or missing, and then falling back to IPv4 DNS.
Note: I am using Windows and XAMPP, however while researching the problem many people have had the same issue on Windows and Mac. Answer for reference for anyone finding this question as I have spent hours trying to find a solution that works for me:
I have tried many solutions for the same problem including putting all of the hosts on one line, removing redundant hosts and virtualhosts, and also including the IPv6 lines - none of these alone were successful.
The only solution which has so far appeared to work for me is a combination of all of the solutions:
Changing the domain I am using from mysite.local to mysite.dev. Inspired by #Cleverlemming's answer.
Including the IPv6 lines.
Removing redundant virtualhosts and hosts (I commented them out).
In my hosts file my hosts are currently on separate lines and so far the issue seems to be fixed.
Good luck to anyone attempting to solve this issue and if anyone has any information to add please do so - this seems to be an issue affected a lot of people with no single known cause or solution.
I had this same problem and finally realized I had the same host entry twice on the same line:
e.g.
127.0.0.1 localhost host1 host2 host3 host4 host5 host1 host6
I removed the second instance of the same host (in the example above - host1) - and things immediately sped up.
Felt a little silly when I discovered this, but when you've got 10 long host names on the same line and you're frequently adding / removing, it can be eaisly overlooked.
The trick that did it for me was adding
127.0.0.1 locahost
on the first line of the host file.
From all my virtual hosts, only the ones using a database were slow. I believe it's because the process of looking up "localhost" for the database connection slowed things down, since I only added the addresses for my virtual hosts and not "localhost" as well. Now it's all snappy again. :)
I've run into this a bunch, too. I have a bunch of vhosts defined on two lines, one for IPv4 and one for IPv6. Moving the host I was trying to resolve to be first in the list sped it up.
127.0.0.1 faster.example.dev host1.example.dev host2.example.dev host3.example.dev host4.example.dev host5.example.dev host6.example.dev
::1 faster.example.dev host1.example.dev host2.example.dev host3.example.dev host4.example.dev host5.example.dev host6.example.dev
A dumb issue that led me to waste some considerable time: after applying #Cleverlemming's answer, I figured out that there were duplicated entries on the hosts file. Something like:
::1 site1.local site2.local site1.local site3.local site4.local
fe80::1%lo0 site1.local site2.local site1.local site3.local site4.local
127.0.0.1 site1.local site2.local site1.local site3.local site4.local
Then IP resolving for site3.local and site4.local takes these 5-seconds of death.
This helped me: Apache HTTP localhost randomly taking 5 seconds on macOS Monterey but fast on HTTPS
Turn off Keep Alive by adding:
KeepAlive Off
To your http.conf