6

I have the following in my C:\Windows\System32\drivers\etc\hosts:

127.0.0.1 example.com

It works when I use: http://example.com

But does not work when I use: https://example.com, which gives error ERR_CONNECTION_REFUSED.

Any idea how to use hosts with an HTTPS site?

If not, any alternative?

  • does example.com exists ? – pun Aug 5 '15 at 19:02
  • Of course ! both http and https ! – yarek Aug 5 '15 at 19:05
  • Of course. It’s specially reserved for use in documentation. However, I don’t see why it wouldn’t work for the OP except perhaps cached DNS information. Or are you referring to other trouble? – Daniel B Aug 5 '15 at 19:06
  • Well: Just test it : edit the C:\Windows\System32\drivers\etc\hosts file and then test with example.com (works fine) and example.com (https version) : gives error ERR_CONNECTION_REFUSED – yarek Aug 5 '15 at 19:12
  • Tunrs out that it would not work. refer to this – pun Aug 5 '15 at 19:20
7

The following in your hosts file

127.0.0.1 example.com

...makes both http://example.com and https://example.com go to 127.0.0.1, hence: your own machine. (Even more: anything that refers to example.com, such as ping or telnet would go to 127.0.0.1 when run from your computer.)

Apparently you have a web server running on your own computer on port 80 (HTTP), but nothing on port 443 (HTTPS). Even more, getting ERR_CONNECTION_REFUSED actually proves your hosts file is used, as otherwise you would see the default website from https://example.com.

Note that if you would have the server on your computer also support HTTPS on port 443, you'd get certificate errors, as there is no way you can buy a certificate for the domain example.com.

  • I run now my apache on port 443 so I can access : 127.0.0.1:443 However example.com is not redirected to my local host. Do you mean if I had a HTTPS running on my localhost, It would not change anything and still have that error ? So NO WAY to do that ? – yarek Aug 5 '15 at 19:43
  • If https://127.0.0.1:443 works, then https://example.com and https://example.com:443 should work too, @yarek. That is: it should give you an "invalid certificate" error/warning as the browser expects the certificate of example.com, but it should be able to connect. I think... Maybe restart the browser (or use another one) to make sure it has not cached the real IP address of example.com. Or try with another domain name. – Arjan Aug 5 '15 at 20:44
  • 1
    (See my edit, @yarek: getting ERR_CONNECTION_REFUSED actually proves your hosts file is used, as otherwise you would see the default website from example.com.) – Arjan Aug 5 '15 at 20:51
  • Ah, @yarek, are you saying you have plain HTTP (not HTTPS) on port 443? That would surely confuse a browser when it tries to open a https:// URL, but then is not able to actually use SSL. (For simplicity, that's kind of the same as using an ftp:// URL in a browser, but the browser then encountering a web server instead of an FTP server on port 21.) – Arjan Aug 5 '15 at 21:55
  • So, @yarek, does the above make sense? – Arjan Aug 7 '15 at 12:13
0

I got tricked thinking that the site serves traffic via https://example.com where in fact the URL is using a subdomain e.g. https://www.example.com In that case just add one more record to the hosts file with www.example.com like below:

127.0.0.1   example.com
127.0.0.1   www.example.com

Or in fact to avoid errors on local web server (if you are running one for development purposes)

0.0.0.0 example.com
0.0.0.0 www.example.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.