Redirect access by IP address to domain name

I’d like to redirect (block?) all access to my NC via IP address and redirect them to domain name - so I DO NOT want to block certain IP addresses from accessing my NC I just want a redirect them and prevent usage of server’s IP address i.e. if my NC server has an IP address of 1.2.3.4 and my domain name is myncdomain.com I want to redirect https://1.2.3.4/login to https://myncdomain.com/login.

I’ve tried adding
RewriteEngine On
RewriteCond %{HTTP_HOST} ^1.2.3.4$
RewriteRule ^(.*)$ https://myncdomain.com/$1 [L,R=301]
to /var/www/.htaccess as well as /var/www/nextcloud.htaccess
but with no effect.

Is this even impossible?

Thanks.

There are several ways, but first: do you have a reverse proxy in front?

Hi jtr,

as far as I’m aware I’m not using reverse proxy.