How to redirect an IP access to domain

How can I setup to redirect an IP access to domain?

For example:
When an user type the server IP address (http://x.x.x.x.x) on the browser, I would like to redirect the user to a domain.

This is what I am trying to do with my TOML file:

    regex = "^http://{IP_ADDRESS}/(.*)"
    replacement = "http://mydomain/${1}"

Please, How can I get this {IP_ADDRESS} variable automatically?

Many thanks in advanced!

So you want any IP to be redirected to the domain?

O’Reilly suggests this regex for IP: ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

So the middleware should be along the lines of

    regex = "^http://((?:[0-9]{1,3}\.){3}[0-9]{1,3})/(.*)"
    replacement = "http://mydomain/${2}"

But I haven't tested it. Also note that you need a router rule to catch them all.

Thank you very much for your time.
The solution to cover all the IP addresses is very clever. I liked it. Thank you very much!

Could you please tell me How would be a router rule for this?
I tried this TOML bellow with no sucess.

      rule = "((?:[0-9]{1,3}\.){3}[0-9]{1,3})"

Thanks and best regards.

You would need something like HostRegexp, see router rule docs.