15

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?

Arjan
  • 30,974
  • 14
  • 75
  • 112
yarek
  • 581
  • 3
  • 8
  • 18
  • does https://example.com exists ? – clhy Aug 05 '15 at 19:02
  • Of course ! both http and https ! – yarek Aug 05 '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 05 '15 at 19:06
  • Well: Just test it : edit the C:\Windows\System32\drivers\etc\hosts file and then test with http://www.example.com (works fine) and https://www.example.com (https version) : gives error ERR_CONNECTION_REFUSED – yarek Aug 05 '15 at 19:12
  • Tunrs out that it would not work. [refer to this](http://stackoverflow.com/questions/3267921/how-to-add-entry-in-windows-host-file-such-that-it-can-redirect-over-https) – clhy Aug 05 '15 at 19:20
  • Sure it would work, @The_IT_Guy_You_Don't_Like. But one would see certificate errors assuming one does not have the right certificate installed. (Not ERR_CONNECTION_REFUSED errors.) – Arjan Aug 05 '15 at 19:22

3 Answers3

12

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.

Arjan
  • 30,974
  • 14
  • 75
  • 112
  • I run now my apache on port 443 so I can access : http://127.0.0.1:443/ However https://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 05 '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 05 '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](http://i.stack.imgur.com/gOorq.png) from https://example.com.) – Arjan Aug 05 '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 05 '15 at 21:55
  • So, @yarek, does the above make sense? – Arjan Aug 07 '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
0

The following in your hosts file, working for me:

127.0.0.1 https://example.com
127.0.0.1 https://www.example.com
Edward
  • 1