A
A
Azigar2019-08-22 22:17:28
Apache HTTP Server
Azigar, 2019-08-22 22:17:28

Automatic subdomains in Apache?

I will say right away that this is not a repetition, because in similar questions I did not find the correct answer.
I have a folder /var/www/mysite.net.ua/www/ . It contains the files of the main site, which opens at mysite.net.ua
In the /var/www/mysite.net.ua folder there are folders /vasya , /kolya , /petya , i.e.:
/var/www/mysite .net.ua/vasya
/var/www/mysite.net.ua/kolya
/var/www/mysite.net.ua/petya
And there can be many such folders. It is necessary to make them open at their addresses respectively:
vasya.mysite.net.ua
kolya.mysite.net.ua
petya.mysite.net.ua
I found this solution: in the /etc/apache2/sites-available/mysite.net.ua.conf
file , I write to the VirtualHost section :

Define root_domain  mysite.net.ua
     Define root_path    /var/www/mysite.net.ua
     
     DocumentRoot  ${root_path}
     ServerName    ${root_domain}

     ServerAlias *.mysite.net.ua
     RewriteEngine On
     RewriteCond %{HTTP_HOST} !^www.mysite.net.ua$
     RewriteCond %{HTTP_HOST} ^((.*)\.)mysite.net.ua$
     RewriteRule ^/(.*) /%2/$1

As a result, any subdomain, for example vasya.mysite.net.ua , will open perfectly, but the main site at mysite.net.ua will not be transferred to the /var/www/mysite.net.ua/www/ folder , but transferred to the root of the main folder site - /var/www/mysite.net.ua . If you put index.php in it, it will open. But it doesn't suit me.
I think about the fact that you need to either write the rule correctly or pervert it by writing the rule in the file /var/www/mysite.net.ua/.htaccess
Please tell me the solution to this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-08-22
@Azigar

<VirtualHost *:80>
    ServerName mysite.net.ua
    ServerAlias www.mysite.net.ua

    DocumentRoot /var/www/mysite.net.ua/www
</VirtualHost>


<VirtualHost *:80>
    ServerName mysite.net.ua
    ServerAlias *.mysite.net.ua

    UseCanonicalName    Off
    VirtualDocumentRoot /var/www/mysite.net.ua/%-4
</VirtualHost>

And connect mod_vhost_alias

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question