Why does typing an IP address instead of the corresponding domain name not show the website?

Solution 1:

Because the proper HTTP Host header is often required to actually get the intended site.

It's very common to host multiple web sites on the same IP address and distinguish between them based on the HTTP Host header specified by the client (as well as the TLS SNI value nowadays in the case of HTTPS).

That is, when you entered http://example.com into your browser the Host header was example.com, but that is not the case when you entered 93.184.216.34. You reach the same web server in both cases, but you receive different responses (in this particular case 200 vs. 404).

Solution 2:

Because usually web servers use "virtual server" technology and are able to answer on your HTTP request within exactly the domain name you request, but not the IP address of the web servers. Thanks to hiding more than one domain name on one IP address.

For example, the Apache web server is able to respond to your HTTP request with an IP address using the section:

<VirtualHost *:80>
ServerName Default
...
</VirtualHost>

or if No VirtualHost used in configuration at all.

The VirtualHost feature in Apache was introduced in 1996.


Solution 3:

In Apache, you can host many websites using just one single IP address. This is called virtual hosting. It's how subdomains can be created, even standalone domains. This is done by setting up an Apache configuration file containing VirtualHost directives for each domain/subdomain.

An example HTTP server that has two virtual hosts, example1.com and example2.com can look like this (IP address definition):

<VirtualHost 93.184.216.34:80>
  ServerName example1.com
  ServerAlias www.example1.com
  DocumentRoot /var/www/example1.com
</VirtualHost>

<VirtualHost 93.184.216.34:80>
  ServerName example2.com
  ServerAlias www.example2.com
  DocumentRoot /var/www/example2.com
</VirtualHost>

It can also look like this (name-based definition):

<VirtualHost *:80>
  ServerName example1.com
  ServerAlias www.example1.com
  DocumentRoot /var/www/example1.com
</VirtualHost>

<VirtualHost *:80>
  ServerName example2.com
  ServerAlias www.example2.com
  DocumentRoot /var/www/example2.com
</VirtualHost>

In both cases, two virtual host records are created internally in memory and used by Apache to compare against when a URI request arrives.

When a user types in the IP address via a user agent, the first virtual host listed in the configuration file is used as the primary domain (i.e. in this case example1.com).

When a user types in a domain name, the request is sent to a public Internet DNS network (ICANN) which provides the IP address associated with it. You registered both via an ICANN registrar (like GoDaddy). You must have both of these correct and give some time before propagation takes hold to all DNS servers on the ICANN network. These days it can take up to 24 hours.

When the request is routed to your Apache HTTP server, the IP address and domain name are matched against the list of internal VirtualHost records. When one is found, the document root is used to form the full filesystem path to the object resource to return back to the user agent. If not, a HTTP 404 is sent along with any error document associated with it.


Solution 4:

I like to use the "house" terminology.

You can quite easily send a letter to a house without a name on it and it'll arrive at the house.

If you put the person's name on it then you're sending to the intended recipient.

The destination is the same but how it is handled when it reaches the house is different.

When you specify the site, i.e. www.example.com then the server knows how to handle the request and which host it is intended for and which site to serve back.