How to prohibit direct access to my site with ip?

Posted August 7, 2020 803 views
UbuntuApacheIPv6Ubuntu 18.04

I have purchased a digital ocean droplet running Apache and have linked it with a domain Currently in some 11 digit IP address after type in the browser like I can view the website and so can the public. My droplet is running on Ubuntu 18.04 LTS. How would I go about only allowing access when typed in and but just make the browser keep on trying but be unable to access directly using the IP address? Thank you.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi there,

What I could suggest is adding a Vhost for your website as described here:

After that in your default Vhost, change the document root so that it does not match the document root of your site.

For example:

  • In your websites Vhost set the document root to /var/www/ and put your website files there

  • And in your default Vhost set the document root to: /var/www/html and put the default Apache index.html file there

That way if someone visits your domain name they would get the content from /var/www/ and if someone visits your IP they would just get the Apache default page from /var/www/html.

Hope that this helps!

by Brennen Bearnes
The Apache web server is the most popular way to serve web content on the internet. Apache has the ability to serve multiple domains from a single server by using a mechanism called "virtual hosts". If a virtual host is configured correctly for each domain, the web server can correctly route traffic to the appropriate files based on the domain name requested. In this guide, we'll demonstrate how to configure Apache virtual hosts on an Ubuntu 16.04 server.
  • thx but not this. Like try visiting this IP in browser, not mine, and it does not load. I want something like this @bobbyiliev

    • Hi there @johnaraon,

      Yes, this can also be achieved by adding a default Vhost. You can then add a deny from all directive directly in that Vhost so that if someone visits the IP they would get a forbidden message.

      Let me know how it goes.