Apache 如何为除localhost之外的所有域提供动态虚拟主机

Apache 如何为除localhost之外的所有域提供动态虚拟主机,apache,.htaccess,configuration,xampp,virtualhost,Apache,.htaccess,Configuration,Xampp,Virtualhost,一点背景知识:我一直想设置动态虚拟主机已经有一段时间了。基本上,我只是希望能够将一个文件夹放到我的虚拟主机文件夹中,并让它在没有任何其他配置的情况下工作。我发现在chrome中,.localhost的任何子域的行为都与localhost相同。这意味着我可以将.localhost用作我所有项目的TLD,而不必为我要添加的每个新虚拟主机编辑我的主机文件 我阅读了上的文档,了解了如何基于主机头拥有动态虚拟主机 在阅读了该页面和internet上的其他资源后,我在httpd vhosts.conf文件中

一点背景知识:我一直想设置动态虚拟主机已经有一段时间了。基本上,我只是希望能够将一个文件夹放到我的虚拟主机文件夹中,并让它在没有任何其他配置的情况下工作。我发现在chrome中,
.localhost
的任何子域的行为都与
localhost
相同。这意味着我可以将
.localhost
用作我所有项目的TLD,而不必为我要添加的每个新虚拟主机编辑我的
主机
文件

我阅读了上的文档,了解了如何基于主机头拥有动态虚拟主机

在阅读了该页面和internet上的其他资源后,我在
httpd vhosts.conf
文件中提出了以下配置。这将使用
.localhost
前面的部分来确定文件夹名称

<VirtualHost *:80>
    ServerAdmin admin@localhost
    
    # Get the server name from the Host header
    UseCanonicalName Off
    
    # Log
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    CustomLog logs/vhost_access.log vcommon
    ErrorLog logs/vhost_error.log
    
    # Match domain name against a folder
    VirtualDocumentRoot "C:/vhosts/%-2+"
    
    <Directory "C:/vhosts/*">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

我有一个解决办法,但它只是解决了你的部分问题

<VirtualHost *:80>
   ServerAdmin admin@localhost
   ServerName  localhost
   DocumentRoot "C:/vhosts/"
</VirtualHost>
<VirtualHost *:80>
   ServerAdmin admin@localhost
   ServerName  longnameyoullneveruse.blabla.localhost
   ServerAlias  *.localhost
   DocumentRoot "C:/vhosts/"
   RewriteEngine On
   RewriteMap lowercase int:tolower
   RewriteCond %{HTTP_HOST} ^(.*)\.localhost$
   RewriteRule ^(.*)$ "C:/vhosts/${lowercase:%1}/$1"
   # you should use all lowercase for subfolders' name
</VirtualHost>

非常感谢你。我只需要为
http://localhost
<VirtualHost *:80>
   ServerAdmin admin@localhost
   ServerName  localhost
   DocumentRoot "C:/vhosts/"
</VirtualHost>
<VirtualHost *:80>
   ServerAdmin admin@localhost
   ServerName  longnameyoullneveruse.blabla.localhost
   ServerAlias  *.localhost
   DocumentRoot "C:/vhosts/"
   RewriteEngine On
   RewriteMap lowercase int:tolower
   RewriteCond %{HTTP_HOST} ^(.*)\.localhost$
   RewriteRule ^(.*)$ "C:/vhosts/${lowercase:%1}/$1"
   # you should use all lowercase for subfolders' name
</VirtualHost>
http://localhost -----------------> C:/vhosts/
http://project1.localhost --------> C:/vhosts/project1
http://project2.localhost --------> C:/vhosts/project2