Reputation: 81
Gethostbyname gives me an ip adress of een website when i use it in the browser i go to that website. If i try the same thing with a website that is on a shared host i get appachi is working correctly.
Here comes my question when i use that ip with gethosbyaddr it gives me an url adress. Why is it that i dont get the website when i use the ip adress in the browser but when i convert it back i get a url that i get the correct website. How does he know what url to give me on a shared host. the ip adress has no 301 redirect.
Upvotes: 1
Views: 134
Reputation: 522513
Shared hosts are hosting several different domains on the same IP address. The way they distinguish between those domains is with the Host
HTTP header. I.e. when visiting such a site, the browser explicitly sends which domain it's looking for in the HTTP request itself. That's why it doesn't work when you visit only the IP.
Therefore, converting such a domain to an IP address is trivial. The other way around is basically impossible, since there are many domains. Which you will get as answer is kind of a lucky draw.
Upvotes: 1
Reputation: 3582
Apache uses virtual hosts, on a shared hosting server there are many websites which resolve to the same IP Address, the virtual hosts then decides which website to show depending on the URL.
So for example:
www.example1.com resolves to 1.2.3.4 - when entered in the address bar, 1.2.3.4 sees that the request is for www.example1.com and shows the files from /var/www/vhosts/example1.com/httpdocs
www.example2.com resolves to 1.2.3.4 - when entered in the address bar, 1.2.3.4 sees that the request is for www.example2.com and shows the files from /var/www/vhosts/example2.com/httpdocs
1.2.3.4 resolves to 1.2.3.4 - when entered in the address bar, 1.2.3.4 sees that the request is for 1.2.3.4 but that doesn't have a record in the virtual hosts, so it shows the 'Apache is working' page by default.
Upvotes: 1