假设有域名domain.com
,已经泛解析子域名*.domain.com
到该主机的ip,web根目录为/var/www/
,
在访问a.domain.com
时,能自动绑定/var/www/a/
目录,访问b.domain.com
时,能自动绑定/var/www/b/
目录
1、分别手动建立相应的virtualhost
主机配置文件vhost.conf
以及/var/www/.htaccess
文件
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^(.*).domain.com$ RewriteCond %{REQUEST_URL} !^/%1/ RewriteRule ^(.*)$ %1/$1 </IfModule>
此时不论以哪个域名访问,都会返回服务器内部错误
如果把RewriteCond %{REQUEST_URL} !^/%1/
这行配置中的%1
替换成具体的字符串,如a
,这样的话,能够实现a.domain.com
绑定到/var/www/a/
目录,但是其他域名依然是/var/www/
目录
2、任意主机名的虚拟主机
RewriteEngine on RewriteMap lowercase int:tolower RewriteCond "${lowercase:%{HTTP_HOST}}" "^www.([^.]+).example.com$" RewriteRule "^(.*)" "/home/%1/www$1"
3、vhost_alias模块实现通配子域名
主机配置文件vhost.conf
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName test.loc ServerAlias *.test.loc # This will be the wildcarded document root. Any folder you create in /var/www/test will be automatically a subdomain name. VirtualDocumentRoot /var/www/test/%-3 # For symlinking your projects and htaccesses to work <Directory /> Options FollowSymLinks AllowOverride All </Directory> ErrorLog /var/log/apache2/vhosts-error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/vhosts-access.log combined </VirtualHost>
https://inchoo.net/magento/how-to-setup-semiautomatic-per-folder-wildcard-subdomains-on-your-local-linux-based-development-computer/
https://httpd.apache.org/docs/2.4/rewrite/vhosts.html