Redirect IP address to domain name on apache

Add the next lines right below :

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)
RewriteRule ^(.)$ https://example.com$1 [R=permanent,L]

Add the next lines right below :

RewriteEngine On
RewriteCond %{HTTP_HOST} !^example.com$
RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)
RewriteRule ^(.)$ https://example.com$1 [R=permanent,L]

more insights