For a pan-resolved domain name, how to automatically bind a subdomain name to a directory with the same name as the subdomain name

  1. does not want to manually create the corresponding virtualhost, based on the sub-domain name, hoping to automatically bind to the directory with the same name through the sub-domain name through the rewriting rule.
    assume that there is a domain name domain.com , and the subdomain * .domain.com has been parsed to the ip of the host. The web root directory is / var/www/ .
    I want to automatically bind the / var/www/a/ directory when accessing a.domain.com , and the / var/www/b/ directory when accessing b.domain.com , and so on.
    is mainly rewriting rules in .htaccess that do not write .
  2. existing host configuration file main.conf

    <VirutualHost *:80>
        DocumentRoot /var/www/
        ServerName domain.com
        ServerAlias *.domain.com
    </VirtualHost>

    and / var/www/.htaccess File

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
        RewriteCond %{REQUEST_URL} !^/%1/
        RewriteRule ^(.*)$ %1/$1
    </IfModule>

    No matter which domain name is accessed at this time, an internal error on the server will be returned

    if RewriteCond% {REQUEST_URL}! ^ /% 1 / in the line configuration 1 is replaced with a specific string, such as a , then a.domain.com can be bound to / var/www/a/ directory, but other domain names are still / var/www/ directory

    .
  3. reference link

I hope I have made my question clear. Please answer it.

May.22,2021

there is no need to write .htaccess , and even this file is not recommended to be enabled.

what you need to see is actually the batch virtual hosts of apache, which can be found in the official documentation of the solution: https://httpd.apache.org/docs.

.

Thank you for @ Feng_Yu 's answer, which is exactly what I want. If you have no experience, you don't even know where to read the documents. I studied the rewrite module all day and tested all kinds of RewriteCond RewriteRule, but didn't get it done.

in addition, I found other solutions:

once again hack Baidu, the things found are stereotyped, and the results included are obviously copied from each other, even the name of the example has not been changed, and no one has ever tested it. Google is very good

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e50eb0-65708.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e50eb0-65708.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?