How block direct ip access apache2

SYSTEM INFORMATION
OS type and version Ubuntu 20.04
Virtualmin version Latest

Hi!
is there a way to deny direct ip access to any website on the server. This server run apache 2.4. I have one server with nginx and there it is simple but I have tried to find a solution for apache.

Put that on top of your root htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^12\.34\.56\.79
RewriteRule ^.*$ - [F,L]
</IfModule>

Replace the numbers with your actual website domain IP.

If HTTPS is enabled, visitors (may) see an unsecured certificate window, but it’s still possible to click through the options to access the website through the direct IP. If so, the 403 response will be displayed.

Alternative:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteRule (.*) https://www.example.com$1 [R=301,L]

Any request that doesn’t contain your website domain, will be force-redirected to your domain.

Or via Apache conf:

This may help as well.

Well that is really the ooposit from what I want. I want no access if people just try the server ip address instead of domain

Hi!
I tested to put that in 000-default.conf which should be where apache should test first but it does not work

Forgot enable it. Works now. perfect thanks

Glad I was able to help. Please mark it as solved.

I was to quick. It blocked also the access via domain name so I had to disable it

opps sorry, misread.

The htaccess variant worked as it should

Thanks

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.