背景知识:我一直想设置动态虚拟主机已有一段时间。基本上,我只是希望能够将一个文件夹放入我的虚拟主机文件夹中,并且无需任何其他配置即可使其正常工作。我发现在chrome中,任何子域的.localhost
行为都与相同localhost
。这意味着我可以将.localhost
其用作所有项目的TLD,而不必HOSTS
为要添加的每个新虚拟主机编辑文件。
我阅读了https://httpd.apache.org/docs/current/vhosts/mass.html上的文档,并弄清了如何基于主机头创建动态虚拟主机。
阅读完该页面和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>
因此,通过此设置,我可以在vhosts
包含所有html文档的文件夹中创建一个新文件夹。因此,例如,如果我创建一个名为的文件夹,则project1
可以通过转到来访问该文件夹http://project1.localhost
。
现在我的主要问题是,我不必总是创建一个新的虚拟主机。我只想创建一个随机的php文件,然后转到即可访问它http://localhost/index.php
。但是,使用上述配置,仅使用会http://localhost
导致错误。可能是因为VirtualDocumentRoot
指令中的模式正在使用的子域,localhost
而当我只使用时没有子域http://localhost
。
根据上述配置,是否有任何方法可以localhost
为的子域提供硬编码的虚拟主机,并为子域提供动态的虚拟主机localhost
?
或者,如何创建一个配置,使我可以拥有此配置:
http://localhost -----------------> C:/vhosts/
http://project1.localhost --------> C:/vhosts/project1
http://project2.localhost --------> C:/vhosts/project2
http://blog.project2.localhost ---> C:/vhosts/project2/blog
我有一个解决方案,但它只能解决您的部分问题。
<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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句