The domain must be available for any non-existent subdomain. I understand that you need to configure DNS so that it sends all requests to the domain, and in the Apache settings specify an alias of the type * .domain.ru, for example. But I also don’t know how to do it correctly.
Tell me how to implement this.
Your question is too general, so it is difficult to give an objective answer. I will indicate the general direction.
The problem is divided into two parts.
First, you need to make sure that when accessing DNS, a certain IP address is given to any subdomain (the default behavior is that there is no entry in the DNS – the answer will not be found).
This part of the question is very extensive … Firstly, with a high degree of probability, you do not hold the zone yourself (two geographically independent class C subnets, your own DNS servers, etc.), but your domain registrar maintains it. All of them, as a rule, combine several services: where the domain was registered, there is also a panel for adding / editing records in the DNS. And secondly, with a high degree of probability, your ISP cannot make it so that some default IP is issued on non-existent records.
What can be done?
The first option is to bring up the DNS server and configure it as you like. I do not give details, since this is for you yourself – choose the type of server (On Windows or Linux), configure it. There will be specific questions – ask separately.
The second option is to look for a provider who will provide you with such a service. Of these providers, I can only name the network solution. I don't know how relevant the example will be in a year / two / three – so you yourself should look for this too.
The second part of the question relates to the web server setup and is independent of the DNS setup. Why independent? Because you can always enter a couple of non-existent subdomains into the hosts file and emulate the configured bind or whatever you have there as DNS.
So this is how it is solved if you have an apache-based server: with configured virtual hosts, one of them will be the default host, and all traffic from unrecognized requests will go to it.
Plus, you can explicitly prescribe, as in this question on en so .
And for a server based on Nginx, everything is done via * in servername:
server_name mydomain.tld *.mydomain.tld;
Or through the default virtual host:
listen 80 default_server;
(To process https, you need to have a wildcard certificate, its price is usually an order of magnitude higher than a regular certificate – and letsencrypt still doesn't seem to issue free wildcards)
Something like that in general terms.
Update. Supplement to the answer to the clarifying question.
I want my site to be available for all sub-domains of the 3rd level and the name of the sub-domain of the 3rd level was included in the server variable, for example, the whole thing is started on the Bitrix virtual machine so that it will configure everything as needed, no problem. I would like to know whether it is realistic to implement it at all?
Of course it's real. PHP has a global variable $ _SERVER and you can always find out the hostname in the dataset. It is passed by the web server, you can take HTTP_HOST and parse it, extracting the subdomain name if you need it.